-1

I have read multiple question about this subject but didn't work for me. Link

I have two model product and price between product and price exist hasMany relationship. in product model:

public function price()
{
   return $this->hasMany("App\Price","product_id","id");
}

I want sort product collection by price. inside my price model i have price property. this is my code for sort products collection by price relation and price property.

$products=Product::find($id);

$products->load(['price'=>function($q){
     $q->orderBy('price', 'asc');
}]);


dd($products->toArray());

please help me.Thanks.

kjones
  • 1,339
  • 1
  • 13
  • 28
mkt
  • 51
  • 1
  • 9

3 Answers3

0

Can you try the following?

$product = Product::with('price', function($query){
  $query->orderBy('price', 'asc');
})->find('id');

dd($product);

Or the following which leverages the collection

$product = Product::find($id);
$sorted = $product->price->sortBy('price');
dd($sorted);
Shobi
  • 10,374
  • 6
  • 46
  • 82
0

in product model:

public function price()
    {
       return $this->hasMany("App\Price","product_id","id")->orderBy('price', 'asc');
    }

This is may not be the proper way. but it will work good.

I made two model with two migrations.

products table
 _ _ _ _ _ _ 
| id | name |
+-----------+
prices table
 _ _ _ _ _ _ _ _ _ _ _ _ _
| id | product_id | price |
+-------------------------+

Then i ensure fillable property on model.

on Product.php
protected $fillable = ['name'];
on Price.php
protected $fillable = ['product_id', 'price'];

And then made a relation on product model.

on Product.php
public function prices()
{
    return $this->hasMany(Price::class, 'product_id', 'id')->orderBy('price', 'asc');
}

Then I call the output.

$products = App\Product::with('prices')->get();

It gives me the desire out put of ascending order as your question above. Recheck these parts,

1. you made your migrations properly.
2. you made the relations properly (check that data comes.).
3. you called them properly.
hasan05
  • 904
  • 5
  • 22
  • "Propper way" - bla. Whatever works. You could also put a global scope on price for the same effect. – Tarek Adam Jun 13 '19 at 17:20
  • @mortezaKazemiTascoo, I am affraid, you may not be mismatch your relation with price table. for sure, have a look that prices table have the column 'price'. – hasan05 Jun 13 '19 at 17:25
  • actually, this also should work. If it doesn't OP have a problem with his tables or the relation – Shobi Jun 13 '19 at 17:26
  • price field exist on table.Thanks – mkt Jun 13 '19 at 17:27
  • the tables migration is too long for show as comment here. – mkt Jun 13 '19 at 17:35
  • One think you can tell me? Both price and product on app folder? I mean, just make sure that, it return some data. – hasan05 Jun 13 '19 at 17:46
  • 1
    "Doesn't work" tells us nothing. If somebody is taking the time to help you with your problem, then you need to give them more information than "doesn't work". – maiorano84 Jun 13 '19 at 17:58
  • This is last i can do. (may be. check the ans and try.) – hasan05 Jun 13 '19 at 18:04
  • Try to make clear, what you have, what you want to. focus on the main problem. If possible give an example, what you want to be output, and what you got. etc etc. it helps someone to understand your problem. – hasan05 Jun 13 '19 at 18:13
-1

lastly i found the solution.

now i can sort products collection according to price collection.

    $products=Product::find($id);
    $products=$products->load("price");
    $products=$products->sortBy(function($product){
        return $product->price;
    });

    dd($products->toArray());

Thanks friends for your helps.

mkt
  • 51
  • 1
  • 9