It looks like you're trying to order the results by the position field within a nested relationship, but the orderBy isn't working as expected. In your query, you're using the with method to eager load the catproduct and rproduct relationships, and then applying the orderBy inside the closure for the rproduct relationship.
However, the issue could arise from the way the query is structured. Here's how you can modify the code to ensure it works properly:
$pr_data = Category::with(['catproduct.rproduct' => function($query) {
$query->where('status', '1')->orderBy('position', 'asc');
}])
->where('id', $categoryDetials->id)
->get();
This code should work if the relationships are set up correctly, but if it's still not working, there are a few things to check:
Category model should have a relationship like:
public function catproduct()
{
return $this->hasMany(CatProduct::class);
}
public function rproduct()
{
return $this->hasMany(RProduct::class);
}
Check the position Field Ensure that the position field exists in the rproduct table and is an integer (or another comparable type).
Use orderBy on the Parent Level If you want to order the Category results as well, you can apply orderBy to the main query:
$pr_data = Category::with(['catproduct.rproduct' => function($query) { $query->where('status', '1')->orderBy('position', 'asc'); }]) ->where('id', $categoryDetials->id) ->orderBy('id', 'asc') // Example: Ordering categories by their ID ->get();