Dan'l
Dan'l

Reputation: 159

I need help defining Laravel Eloquent relationships

The two models:

class Event extends Model
{  // serves the same role as Post 
    public function memoryof()
    {  
        return $this->hasMany(MemoryOf::class);
    }
}



class MemoryOf extends Model
{  // serves the same role as comments
    public function event()
    {
          return $this->belongsTo(Event::class);
    }
    
}

I retrieve the data like this:

$events=Event::with('memories')->get()->sortByDesc('created_at');

This SHOULD give me a collection. Instead it gives:

"Property [memoryof] does not exist on this collection instance."

Please help me to fix this so that I get a collection.

Upvotes: 2

Views: 43

Answers (1)

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

Please try this

$events = Event::with('memoryof')->get()->sortByDesc('created_at');
dd($events)

Upvotes: 1

Related Questions