Similar in what way? The same number of letters in the name? like 'brown' and 'green' ?
Trying to get products with minimum of 3 similar colors
This code is returning products with minimum of three colors, how can I get the similar ones, any advice will help. Thanks to all
$sel_products = Product::has('colors', '>=', 3)->get();
@Snapey Yes
@andrei.girnet96@gmail.com
As what I've understood so far that you are trying to return colors that has specific string length, if that so then you can use strlen() to achieve what you want.
@RutyCodes Hmm that is not going to work, if the lenght of the colors are similar, I want to return products which have 3 colors minimum and all the colors being the same
Is there a name or title attribute in the colors table and you want to filter by that attribute ?
@orest There is a field name for the colors
If this is the case maybe you can do
Product::whereHas('colors', function($query){
return $query->whereIn('name', ['red', 'blue'])->havingRaw('count(*) >= ?', [3])->groupBy('product_id');
})->get();
@orest I am getting this error Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Illuminate\Database\Eloquent\Builder given, called in C:\xampp\htdocs\test\vendor\laravel\framework\src\Illuminate\Http\Response.php on line 72
@andrei.girnet96@gmail.com I updated it. Can you check again ?
@orest It worked, but it is returning just one product, it should return two of them as just two products have 3 colors and they are the same
What is the relationship between the products and colors ?
@orest Many to many
Can you show the code that you are running ?
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');
}
}
public function index()
{
$products = Product::all();
$colors = Color::all();
$selected_products = Product::has('colors', '>=', 3)->get();
return view('welcome', compact('products','colors','selected_products'));
}
Why do you need to get products which have a color with the same number of letters?
Explain for what it's good for?
@MichalOravec It is just a task I need to complete from school, I I have spent all my day serching how but I havent found the answer, there is not much in internet about this. I need products that have 3 colors and the colors are the same for example : Tshirt - blue, yellow, red Jeans- blue, yellow, red
Could you please at least tell me where to dig deeper, where to search, or what to learn
Are you sure there is nothing wrong with your data ? Because the code that we have posted should do the job.
@orest I am sure that I have got 2 products with the same color and more than 3 colors
Most likely this is what you need:
$colors = ['blue', 'yellow', 'red'];
$products = Product::whereHas('colors', function ($query) use ($colors) {
$query->distinct()->whereIn('name', $colors);
}, '=', count($colors))->get();
@MichalOravec It is working but is returning just one record, but I have two items with the this colors
@andrei.girnet96@gmail.com We don't know what data you have in the database.
the string length bit was me taking the mickey out of the requirement of "how can I get the similar ones"
I asked similar how? I was not expecting a response of 'yes'
@Snapey Yeah I know, when I saw his response "yes", in my head was like: "why is it good for?!"
Sometimes it's very difficult to help other people with their issues.
Thanks for help everybody!!
Please or to participate in this conversation.