如何从数据库查询买家订单到他们的个人资料

时间:2019-06-17 17:54:58

标签: php laravel laravel-5 eloquent

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

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

控制器

 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

User.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');
}

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

任何帮助将不胜感激

0 个答案:

没有答案