i have 3 tables , colors , products and color_product with many to many relation , my problem is that i have a filter where the user can choose more than one color with the rest of the filter options how i filter the data with the selected colors, code will explain more
http://prntscr.com/l0vzim
http://prntscr.com/l0vzy8
http://prntscr.com/l0w02x
http://prntscr.com/l0w093
public function filter(Request $request)
{
$sections = Section::all();
$section = $request->section_id;
$min = $request->min;
$max = $request->max;
$colors = $request->color_id;
$products = Product::where('is_active', 1);
if ($section)
$products->where('section_id', $section);
if ($min)
$products->where('price_after', '>=', $min);
if ($max)
$products->where('price_after', '<=', $max);
// if ($colors)
// foreach ($colors as $color) {
//
//
//
//// $color = Color::where('id', $color)->first();
//// $checker = $color->product;
//// foreach($checker as $products)
////// $products = $product->where('is_active', 1)->get();
//// if ($products->is_active == 1)
//// if ($section)
//// $products->where('section_id', $section);
//// if ($min)
//// $products->where('price_after', '>=', $min);
//// if ($max)
//// $products->where('price_after', '<=', $max);
// }
$filter = $products->paginate(9);
return view('site.filter', compact('filter', 'sections'));
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable=[
'name',
'code',
'price_before',
'price_after',
'quantity',
'section_id',
'category',
'description',
'img',
];
public function color()
{
return $this->belongsToMany('App\Color');
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Color extends Model
{
protected $fillable=[
'name',
];
public function product()
{
return $this->belongsToMany('App\Product');
}
}