Still haven't solved it, trying to extract Products with 3 common colors(products having the same color but not less than three)
Can I use somehow Eloquent or Laravel queries to do this task, Sorry guys I just can't find the solution anywhere in the internet.
Thanks for advance for any contribution I will really appreciate.
I will try to provide as many info as I can:
So I have many to many relationship between Products and Colors tables.
class Product extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function colors(){
return $this->belongsToMany(Color::class,'colors_products');
}
}
class Color extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function products(){
return $this->belongsToMany(Product::class,'colors_products');
}
}
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
$colors = Color::all();
$selected_products = Product::has('colors', '>=', 3)->get();
return view('welcome', compact('products','colors','selected_products'));
}