79393093

Date: 2025-01-28 08:14:18
Score: 1
Natty:
Report link

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:

  1. Check Relationship Definitions Ensure that the relationships in your Category model (catproduct and rproduct) are correctly defined. For example:

Category model should have a relationship like:

public function catproduct()
{
 return $this->hasMany(CatProduct::class);
}
public function rproduct()
{
 return $this->hasMany(RProduct::class);
}
  1. Check the position Field Ensure that the position field exists in the rproduct table and is an integer (or another comparable type).

  2. 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();

Reasons:
  • Blacklisted phrase (2): still not working
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: mehdi musavi