Reputation: 73
I have two model: Category and PostAd model. They two have relation with each other. In laravel view i have passed data through Category Model to display PostAd data. I want to apply OrderBy condition in query but it doesn't work
Code
$data['category'] = Category::with(['postads'])->where('id',$id)->get();
I want to do this
$data['category'] = Category::with(['postads'])->where('id',$id)->orderBy('adtitle','DESC')->get();
But it doesn't work because it is Category Model. How can i solve this.
Upvotes: 1
Views: 66
Reputation: 15316
if you want to get PostAd data with an order by then you can do an eager load Order by.
And here you're using get()
method so it should be first();
because ->where('id',$id)
always get one record.
$data['category'] = Category::with(['postads' => function($query) {
$query->orderBy('adtitle', 'DESC');
}])->where('id',$id)->first();
Upvotes: 3