Laravel Join Query to get all matching records


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.*',' as vendor_name','product_discounts.start_time',
            'product_discounts.end_time', 'product_discounts.discount_type','product_discounts.discount_value','','product_attributes.value')
            ->joinSub($product_attributes, 'product_attributes', function ($join) {
                $join->on('', '=', 'product_attributes.product_id');

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

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


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