Reputation: 25
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;
}
i am adding a query here which is working fine:
$output_product = $currUser->products()->where('product_id', $product_id)->first();
Thanks
Upvotes: 1
Views: 1348
Reputation: 658
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
Upvotes: 3
Reputation: 81
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
Upvotes: 0