2

I have a product table and a sub_product table.

In product table there are many global field like name and cat_id . In sub_product table there are product_id, color and Price

Now I want add a filed only to product laravel model(collection) that give me minimum price for each product fromsub_product. how can I do that?

3
  • I think you have to declare a variable in product, say $minPrice and in controllorer you have to calculate value of this variable by checking price of each subproduct . Commented Jul 13, 2017 at 5:55
  • Also, if you have some code you tried, please share it with us. Commented Jul 13, 2017 at 5:56
  • @Diabolus no I dont want calculate in controller. only just like a filed in table that there is every where and every relation. Commented Jul 13, 2017 at 6:07

2 Answers 2

1

In Product class, define relationship:

public function subproducts()
{
    return $this->hasMany(SubProduct::class);
}

Define an accessor method:

public function getMinimumPriceAttribute()
{
    return $this->subproducts()->min('price');
}

Retrieve value as field:

$price = $product->minimum_price;
Sign up to request clarification or add additional context in comments.

3 Comments

protected $appends = array('min_price'); add this in model. when use ->toArray() can see this filed. do you know how can I show this filed in collection view inside of attributes? thank you.
@HamidNaghipour, how do you want to use it with in collection? For example, what would the code look like?
@HamidNaghipour, depending on the collection method, you could use a closure, and access $product->minimum_price inside that.
0

i think you should define a relationship in product model with sub_product model and then you can query the product based on this relationship. THe relationship will be one to many.

 public function hasSubPRoducts(){
    return $this->hasMany('\App\SubProduct','product_id','id');
    }

then query the model

 \App\Products::where('products_id',$id)-
>with(['hasSubPRoducts',function($query){
$query->orderBy('price', 'DESC');
}
])->get()->first();

5 Comments

thank you. what about when I want get like this : \App\category->with("Products") ?
then you would user nested relationship using Dot operator \App\category->with("Products.hasSubPRoducts")
you mean like this?! \App\category::- >with(['Products.hasSubPRoducts',function($query){ $query->orderBy('price', 'DESC'); } ])->get()->first(); I think this is wrong!
\App\category::find($category_id)->with(['Products.hasSubPRoducts',function($query){ $query->orderBy('price', 'DESC'); } ])->get()->first();
\App\category::where('cat_id',2)->with(['Products.hasSubPRo‌​ducts',function($que‌​ry){ $query->orderBy('price', 'DESC'); } ])->get()->first(); where cat_id will be name of category id column in your table this will work like charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.