Home > Enterprise >  I want make array for eloquent relationship laravel
I want make array for eloquent relationship laravel

Time:01-16

in Course model this relation are include

public function course_modules()
    {
        return $this->hasMany(CourseModule::class, 'course_id');
    }

    public function course_lessons()
    {
        return $this->hasMany(CourseLesson::class, 'course_id');
    }

    public function course_contents()
    {
        return $this->hasMany(CourseContent::class, 'course_id');
    }

i want to make a array for hasMany relation like

$hasMany=[
    CourseModule::class,
    CourseLesson::class
]

CodePudding user response:

I wanted to do this for fun, turned out pretty difficult, but here you go, there are some requirements you need to make sure of, but it gets the job done, I will be using a mix of PHP & Laravel to accomplish this.

Step 1: Make sure your main class has proper return method types. So in your case.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Course extends Model
{    
    public function course_modules() : HasMany
    {
        return $this->hasMany(CourseModule::class, 'course_id');
    }

    public function course_lessons() : HasMany
    {
        return $this->hasMany(CourseLesson::class, 'course_id');
    }

    public function course_contents() : HasMany
    {
        return $this->hasMany(CourseContent::class, 'course_id');
    }
}

Step 2: In your controller, you need to use ReflectionClass, would love if someone actually can improve this for learning purposes.

<?php

namespace App\Http\Controllers;

use ReflectionClass;

class CourseController extends Controller
{
    public function test(){
        //We will build a hasMany array
        $hasMany = [];
        
        //here we will use ReflectionClass on our primary class that we want to use.
        $reflection = new ReflectionClass(new \App\Models\Course);
        
        //Lets loop thru the methods available (300  i don't like this part)
        foreach($reflection->getMethods() as $method){
            
            //if the method return type is HasMany
            if($method->getReturnType() != null && $method->getReturnType()->getName() == 'Illuminate\Database\Eloquent\Relations\HasMany'){
                //we grab the method name
                $methodName = $method->getName();
                //then we finally check for the relatedClass name and add to the array
                array_push($hasMany, get_class(($instance = new Course)->$methodName()->getRelated()));
            }
        }
        
        //lets dump to see the results
        dd($hasMany);
    }

Results: an array of the classes :D

array:2 [▼ 
  0 => "App\Models\ProgramTest",
  1 => "App\Models\ProgramAnotherTest"
]

CodePudding user response:

According to syntax, we are not able do this in Laravel. However, you can use an model mutors to solve this issue.

public function getCourseDetailsAttribute(){
  $arr=[
     "course_modules"=>$this->course_modules(),
     "course_lessions"=>$this->course_lessons(),
  ];
  return $arr;
}

In the Controller you can write like this,

$course=Course::find(1)->course_details;

For more details t; https://laravel.com/docs/5.7/eloquent-mutators

  • Related