dimanche 31 décembre 2017

Laravel-5 whereHas Clouse dosen't work as expected

I have categories and products tables. They are related with Many to Many relationships.. I want to get products by categories but whereHas clause dosen't work

$products = Product::whereHas('categories',function ($query) use ($slug){
$query->where('category_name',$slug);
})->whereBetween('price',[100,200])->get();

Category Model;

public function products(){
return $this->belongsToMany('App\Product','category_product','category_id','product_id');
 }

Product Model;

public function categories() {
return $this->belongsToMany('App\Category','category_product','product_id','category_id');
    }

categories table

So, what is my mistake in here ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire