LipeTuga
LipeTuga

Reputation: 605

Strange behavior laravel 6.2 collections on php 7.4

I recently updated updated my version of php 7.3 to 7.4 and i noticed a strange behavior on Laravel version 6.* collections. The following piece of code works:

    $modules = Module::orderByPriority()->get()->transform(function($module){

        dd(['id'=>$module->id, 'name'=>$module->course->name.' - '. $module->name]);
    });

And this don't work:

    $modules = Module::orderByPriority()->get()->transform(function($module){

         return ['id'=>$module->id, 'name'=>$module->course->name.'-'. $module->name];
    });

returning always the given error: Trying to get property 'name' of non-object

Upvotes: 0

Views: 73

Answers (1)

Arun A S
Arun A S

Reputation: 7006

Your dd() stops the iteration at the first iteration of the transform() method. Most likely, you have a module that does not have a course, hence during its iteration, you get an error because $module->course is null, hence trying to access name property on null gives you the error

Upvotes: 1

Related Questions