I am trying to limit the results of this query:
$gallery = Event::with('EventsMedia')->orderBy('start', 'desc')->get();
currently it shows all the events with their images. What i want is to show only 3 results. I tried writting it like this:
$gallery = DB::table('events')
->join('events_media', 'events.id', '=','events_media.events_id')
->orderBy('start', 'desc')
->take(4)
->get();
But my events and their images ended up not showing at all.
Please advise.
CodePudding user response:
Not sure why you had to change the query. You could've simply written it like this, just by adding limit(4)
in the end:
$gallery = Event::with('EventsMedia')->orderBy('start', 'desc')->limit(4)->get();
See if that works.
CodePudding user response:
You can try use relations query builder.
$gallery = Event::with('EventsMedia', function($query) {
$query->limit(3)
})->orderBy('start', 'desc')->get();