sharkyenergy
sharkyenergy

Reputation: 4173

Laravel - Has many relationship not working inside foreach loop

I have a HasMany relationship that works on the whole site. Just made a new controller and in this controller I have this inside a method:

  // find all galleries that need to be deleted...
    $galleries = DB::table('galleries')->where('curdeldate', '<', time())->get();

    foreach($galleries as $gallery)
    {            

        //get all the images associated with the gallery
        $photos = $gallery->photos;

this throws this message: ErrorException Undefined property: stdClass::$photos but I cannot figure out why...

I also included this:

use App\Gallery;
use App\Photo;

Upvotes: 1

Views: 859

Answers (1)

lewis4u
lewis4u

Reputation: 15047

You should use:

$galleries = Gallery::where('curdeldate', '<', time())->get();

foreach($galleries as $gallery)
{            
    $photos = $gallery->photos;

.............

Upvotes: 1

Related Questions