如何从包含 product_id in laravel 的订单 table 中获取产品名称?
How to fetch product name from orders table which contain product_id in laravel?
我无法根据匹配的 ID 获取产品名称,但我在 table 中得到了相同的名称,如下所示
this is the ordered list of products
请查看下面显示的 table 以供参考 (order_list table)
order_list table
Table 个产品
products table
我的控制器
public function details($id)
{
$orders = Order_model::find($id);
$Order_lists = Order_list::orderBy('id', 'asc')->where('user_id', '=', Auth::user()->id)->where('order_id', '=', $id)->get();
foreach($Order_lists as $order_list)
{
$prod_name = DB::table("products")->where('id', $order_list->product_id)->value('name');
}
$address = DB::table("delivery_address")->where('user_id', '=', Auth::user()->id)->get();
if(Auth::user()->role == 'customer')
{
return view('customer.orders.details',['Order_lists' => $Order_lists, 'orders' => $orders, 'address' => $address, 'prod_name' => $prod_name]);
}
}
请帮助我,因为我正处于学习阶段,任何帮助都会很高兴听到谢谢。
问题是 $prod_name 是单个变量,您 运行 在循环中使用它。所以它只替换每次迭代并只获取最后一次迭代名称。所以如果你想用 $order_list 获取每个产品名称,你可以轻松地为产品 table 创建模型。然后
创建一对一 Order_list。例如:
https://laravel.com/docs/7.x/eloquent-relationships#one-to-one
class Order_list extends Model
{
public function products()
{
return $this->hasOne('App\Products',"product_id"); //your model name and path
}
}
那么您可以像这样获取所有产品的订单列表数据:
$Order_lists = Order_list::orderBy('id', 'asc')->where('user_id', '=', Auth::user()->id)->where('order_id', '=', $id)->with('products')->get();
产品详情应在$Order_lists[0]->products->name
编辑:运行 blade 文件中的这个
@foreach($Order_lists as $Order_list)
////other <td>s
<td>{{$Order_list->products->name}}</td>
@endforeach
如果上述方法比较复杂,您可以创建单独的数组来命名
$prod_name =[];
foreach($Order_lists as $order_list)
{
$prod_name[$order_list->product_id] = DB::table("products")->where('id', $order_list->product_id)->value('name');
}
然后像这样在 blade 文件上阅读它:
{{$prod_name[$order_list->product_id]}}
编辑:
要以第二种方法打印元素,您可以使用 blade 语法。
首先将变量发送到 .blade 文件
例如:view('your.blade.php,compact('Order_lists','prod_name'));
//then print in .blade file
@foreach($Order_lists as $Order_list)
////other <td>s
<td>{{$prod_name[$order_list->product_id]}}</td>
@endforeach
我无法根据匹配的 ID 获取产品名称,但我在 table 中得到了相同的名称,如下所示 this is the ordered list of products
请查看下面显示的 table 以供参考 (order_list table)
order_list table
Table 个产品
products table
我的控制器
public function details($id)
{
$orders = Order_model::find($id);
$Order_lists = Order_list::orderBy('id', 'asc')->where('user_id', '=', Auth::user()->id)->where('order_id', '=', $id)->get();
foreach($Order_lists as $order_list)
{
$prod_name = DB::table("products")->where('id', $order_list->product_id)->value('name');
}
$address = DB::table("delivery_address")->where('user_id', '=', Auth::user()->id)->get();
if(Auth::user()->role == 'customer')
{
return view('customer.orders.details',['Order_lists' => $Order_lists, 'orders' => $orders, 'address' => $address, 'prod_name' => $prod_name]);
}
}
请帮助我,因为我正处于学习阶段,任何帮助都会很高兴听到谢谢。
问题是 $prod_name 是单个变量,您 运行 在循环中使用它。所以它只替换每次迭代并只获取最后一次迭代名称。所以如果你想用 $order_list 获取每个产品名称,你可以轻松地为产品 table 创建模型。然后 创建一对一 Order_list。例如:
https://laravel.com/docs/7.x/eloquent-relationships#one-to-one
class Order_list extends Model
{
public function products()
{
return $this->hasOne('App\Products',"product_id"); //your model name and path
}
}
那么您可以像这样获取所有产品的订单列表数据:
$Order_lists = Order_list::orderBy('id', 'asc')->where('user_id', '=', Auth::user()->id)->where('order_id', '=', $id)->with('products')->get();
产品详情应在$Order_lists[0]->products->name
编辑:运行 blade 文件中的这个
@foreach($Order_lists as $Order_list)
////other <td>s
<td>{{$Order_list->products->name}}</td>
@endforeach
如果上述方法比较复杂,您可以创建单独的数组来命名
$prod_name =[];
foreach($Order_lists as $order_list)
{
$prod_name[$order_list->product_id] = DB::table("products")->where('id', $order_list->product_id)->value('name');
}
然后像这样在 blade 文件上阅读它:
{{$prod_name[$order_list->product_id]}}
编辑: 要以第二种方法打印元素,您可以使用 blade 语法。 首先将变量发送到 .blade 文件 例如:view('your.blade.php,compact('Order_lists','prod_name'));
//then print in .blade file
@foreach($Order_lists as $Order_list)
////other <td>s
<td>{{$prod_name[$order_list->product_id]}}</td>
@endforeach