In my laravel project I am loading all the posts, posts author, replies and replies author using eager loading feature.
$pin = Posts::with('author', 'replies', 'replies.author')->find($pin_id);
Here is Replies model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Replies extends Model
{
public function replyable()
{
return $this->morphTo();
}
public function author()
{
return $this->belongsTo('App\User', 'author_id');
}
}
Is it possible to sort the replies
in descending order by its(replies) id while we fetch all the posts using the eager loading feature.
I think, this should help.
Posts::with(['replies' => function($query) {
$query->orderBy('id');
}])->find($pin_id);