0
$posts =  PostUrl::with(['post' => function ($q) {
    $q->orderBy('created_at', 'DESC');
}])
->where('category','like' ,'%'.$cat.'%')
->paginate(8);

How can I sort the result as per the created_at of the post inside?

When try to ->orderBy('post.created_at','DESC') it show column not found.

2 Answers2

0

Make sure you have established correctly in your PostUrl model the relationship with your Post model (if it is so called). You should have a function similar to the following in your model in order to with() method works correctly in the controller:

public function post(){
        return $this->hasMany('App\Post', 'postid');
    }

This way there should be no problem for the orderBy clause to work properly.

jssDev
  • 923
  • 5
  • 18
0

Add PostUrl models

public function post(){
    return $this->hasMany('App\Post', 'postid')->orderBy('created_at', 'DESC');
}

Controller :

$posts =  PostUrl::with(['post'])->where('category','like' ,'%'.$cat.'%')->paginate(8);
Yunus Kocabay
  • 133
  • 10