首页 > 解决方案 > 如何从数据库查询买家订单到他们的资料

问题描述

我正在尝试从数据库查询买家订单到他们的个人资料(仪表板),所以当买家下订单时,他应该在他的仪表板中看到订单信息。

我的代码运行不正常,我该如何解决这个问题。

控制器

 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);
 }
 }

任何帮助将不胜感激

标签: phplaravellaravel-5eloquent

解决方案


推荐阅读