php - 如何从数据库查询买家订单到他们的资料
问题描述
我正在尝试从数据库查询买家订单到他们的个人资料(仪表板),所以当买家下订单时,他应该在他的仪表板中看到订单信息。
我的代码运行不正常,我该如何解决这个问题。
控制器
public function myOrders(User $user)
{
$orders=Order::where('buyer_id',$user->id)->get();
return view('myOrders', compact('user','orders'));
//dd($orders);
}
刀片视图
@foreach($orders as $sell)
<tr>
<td>{{$sell->orders}}</td>
<td>{{$sell->products}}</td>
<td>{{$sell->created_at}}</td>
<td>{{$sell->order->shipping_name}}</td>
<td>{{$sell->order->shipping_city}}</td>
<td>{{$sell->order->shipping_phone}}</td>
<td>
<a href="">View Order Details</a>
</td>
</tr>
@endforeach
用户.php
public function orders()
{
return $this->hasManyThrough(Order::class, Products_model::class, 'buyer_id', 'seller_id', 'product_id');
}
public function orderFromBuyers()
{
return $this->hasManyThrough(OrderProduct::class, Products_model::class, 'buyer_id', 'product_id');
}
public function orderFromSellers()
{
return $this->hasManyThrough(OrderProduct::class, Products_model::class, 'seller_id', 'product_id');
}
订单产品.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderProduct extends Model
{
protected $table = 'order_product';
protected $fillable = ['order_id', 'buyer_id', 'seller_id','product_id', 'quantity'];
public function products()
{
return $this->belongsTo('App\Products_model');
}
public function buyer()
{
return $this->belongsTo(User::class, 'id', 'buyer_id');
}
public function seller()
{
return $this->belongsTo(User::class, 'id', 'seller_id');
}
public function order()
{
return $this->belongsTo(Order::class);
}
}
任何帮助将不胜感激
解决方案
推荐阅读
- c - 如何使用 scanf 读取用户输入并根据用户键入的内容创建字符串?
- javascript - 未能“动态”导入 date-fns/locale 库 - TypeScript 给出尝试导入错误
- javascript - Jquery:在动态类下查找复选框
- unity3d - Y轴上的Unity旋转精灵在游戏中不平衡
- python - 协助我的代码 - 自动化仓储库存控制
- apache-superset - 超集错误“您的仪表板太大。请在保存前减小大小”
- .htaccess - 将 url 路径转换为查询字符串,同时使用 .htaccess 保留旧查询字符串
- vba - VBA 过滤数据 - 组合框刷新
- java - 如何将图像 url 设置为 Subsampling-Scale-ImageView?
- javascript - 在 React 中添加按钮以重置表中的值