1

I want to retrieve data from multiple rows through Laravel Eloquont:

Here is what i have tried:

        $currUser = Auth::User();
        ### search
        
        
        $output_product = $currUser->table('products')
            ->join('product_variants', 'products.product_id', '=', 'product_variants.product_id')
            ->join('product_images', 'product_vairants.id', '=', 'product_images.vairant_id')
            ->select('products.title', 'products.description', 'product_variants.price', 'product_variants.quantity', 'product_images.imgurl')->where('products.product_id', $product_id)
            ->get();        
        return $output_product;
                        
        }

But this function is not returning anything,

i am adding a query here which is working fine:
$output_product = $currUser->products()->where('product_id', $product_id)->first();

Thanks

1
  • typos in your second join (vairant)? Commented Aug 14, 2020 at 12:00

2 Answers 2

3

Thorugh query builder you can do this like:

$output_product = DB::table('products')
->select('products.title', 'products.description', 'product_variants.price','product_variants.quantity', 'product_images.imgurl')
->join('product_variants', 'products.product_id', '=', 'product_variants.product_id')
->join('product_images', 'product_vairants.id', '=', 'product_images.vairant_id')
->where('products.product_id', $product_id)
->get();
return $output_product;

And if you want to do with Eloquont.you need to define all the relationships in the respective Model classes

Sign up to request clarification or add additional context in comments.

Comments

0

You should do it like;

DB::table('')....

make a join with user-s table and in the end and a where user id is equal to $currUser->id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.