Laravel Join Query to get all matching records

Published

i want to get al product attributes (if any) for the given product id using a sub query in join.

Below is my query

 $product_attributes = ProductAttributes::select('name as attribute_name', 'value as attribute_value')->get();

 $data['product']= Product::select('products.*','vendors.name as vendor_name','product_discounts.start_time',
            'product_discounts.end_time', 'product_discounts.discount_type','product_discounts.discount_value','product_attributes.name','product_attributes.value')
            ->leftjoin('product_discounts','products.id','=','product_discounts.product_id')
            ->joinSub($product_attributes, 'product_attributes', function ($join) {
                $join->on('products.id', '=', 'product_attributes.product_id');
            })->leftjoin('vendors','products.vendor_id','=','vendors.id')->where('id',$request->product_id)->first(); 

This query gives me following error

InvalidArgumentException A subquery must be a query builder instance,
a Closure, or a string.

if i use simple leftjoin with product attributes it gives me on last item from the product_attributes table .

can someone please guide me how can I list all the product attributes against the product without loop . Below is the table for product attributes

enter image description here

Source: Laravel

Published
Categorised as eloquent, join, laravel, sql, subquery Tagged , , , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq