Skip to content
Advertisement

How to fetch product name from orders table which contain product_id in laravel?

I am unable to fetch the product name according to matching id but i am getting same name in table as shown below this is the ordered list of products

Please check table for reference ill be showing below (order_list table)

order_list table

Table products

products table

My Controller

JavaScript

please Help me out as i am at learning stage and any help would be glad to hear thanks.

Advertisement

Answer

the problem is $prod_name is a single variable and you running it in loop. so it only replace every iteration and just get last iteration name. so if you want get every product name with $order_list , you can easily create Model for products table. then create one to one Order_list. eg:

https://laravel.com/docs/7.x/eloquent-relationships#one-to-one

JavaScript

then you can get all order list data with product like this:

JavaScript

product details should in $Order_lists[0]->products->name

edit: to run this in blade file

JavaScript

if above method is complex you can create separate array to name

JavaScript

then read it on blade file like this:

JavaScript

edit: to print elements in second method you can use blade syntax. first send veriables to .blade file eg: view(‘your.blade.php,compact(‘Order_lists’,’prod_name’));

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement