mercredi 9 décembre 2020

Product not shown with relation in Laravel

I have this two tables:

One is Product Table :

id Title Price
1 Title 1 5000
2 Product 2 7000

and the other is product attribute table:

id product_id attribute_id attribute_name value
1 1 5 Color Red
2 1 6 Size XL
3 2 5 Color Green

Product and Product attribute is related with following relation (In the product model):

 public function attributes()
    {
        return $this->hasMany(ProductsAttribute::class, 'product_id ');
    }

I'm fetching data like this :

return Product::with('attributes')
        ->whereHas('attributes', function ($query) use ($attribute_id,$attribute_value){
            $query->whereIn('attribute_id', $attribute_id);
            $query->whereIn('value', $attribute_value);
        })
        ->paginate(10);

Issue is if there is no attribute in the product attribute table which are related to a particular product then that product is not shown in the search, but my requirement is if related attribute is not there then also product should be shown in the result.

Any help is highly appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire