skip to Main Content

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
]

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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 AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use IlluminateDatabaseEloquentRelationsHasMany;
    
    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 AppHttpControllers;
    
    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 AppModelsCourse);
            
            //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() == 'IlluminateDatabaseEloquentRelationsHasMany'){
                    //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 😀

    array:2 [â–¼ 
      0 => "AppModelsProgramTest",
      1 => "AppModelsProgramAnotherTest"
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search