Php 买方下的订单不会向卖方显示Laravel

Php 买方下的订单不会向卖方显示Laravel,php,laravel,eloquent,laravel-5.8,Php,Laravel,Eloquent,Laravel 5.8,我试图让买家下的订单被相应的卖家看到,但它没有显示任何东西 我已经试了好几个小时来解决这个问题,但我还是被卡住了 这是我的模型Order.php <?php namespace App; use Illuminate\Database\Eloquent\Model; class Order extends Model { //protected $table = 'orders'; protected $fillable = [ 'user_id',

我试图让买家下的订单被相应的卖家看到,但它没有显示任何东西

我已经试了好几个小时来解决这个问题,但我还是被卡住了

这是我的模型Order.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    //protected $table = 'orders';
    protected $fillable =  [
        'user_id', 'shipping_email', 'shipping_name', 'shipping_city',    'shipping_phone', 'billing_subtotal', 'billing_total',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function products()
    {
        return $this->belongsToMany('App\Products_model')->withPivot('quantity');
    }

    public function orders(){
        return $this->hasMany('App\OrderProduct', 'order_id');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderProduct extends Model
{
    protected $table = 'order_product';
    protected $fillable = ['order_id', 'product_id', 'quantity'];

    public function products()
    { 
        return $this->belongsTo('App\Products_model');
    }
}
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'Seller'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token', 
    ];

    public function products()
    {
        return $this->hasMany(Products_model::class);
    }

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function orders()
    {
        $this->hasManyThrough(Order::class, Products_model::class, 'user_id', 'product_id');
    }
}
我需要列出产品的每个卖家(用户)在其他买家(用户)购买时收到订单。到目前为止,它显示
“订单ID订单日期客户姓名客户城市客户电话

在订单型号上,您与用户有关系,这意味着您拥有买家信息。 但你和卖家没有任何关系

因此,为卖方添加一个关系

注:我假设买方和卖方都是
用户

您必须添加买家的关系和卖家的关系(别忘了在数据库中也添加它们。这意味着您需要在
订单表中添加
买家id
卖家id
字段)

//订单查看功能

public function viewOrders(User $user) {
    $products = Products_model::where('user_id', '=', $user->id)->get();
    // all sells
    $sells = $user->sells;
    // all buys
    $buys = $user->buys;

}
在视图中,可以像这样循环

@foreach($sells as $sell) 
    {{ $sell->orders }} //for orders.
    {{ $sell->products }} //for product
    @foreach($sell->orders as $order)
        {{ $order->product }} //single product
    @endforeach
@endforeach

在订单模型中,您与用户有关系,这意味着您拥有买家信息。 但你和卖家没有任何关系

因此,为卖方添加一个关系

注:我假设买方和卖方都是
用户

您必须添加买家的关系和卖家的关系(别忘了在数据库中也添加它们。这意味着您需要在
订单表中添加
买家id
卖家id
字段)

//订单查看功能

public function viewOrders(User $user) {
    $products = Products_model::where('user_id', '=', $user->id)->get();
    // all sells
    $sells = $user->sells;
    // all buys
    $buys = $user->buys;

}
在视图中,可以像这样循环

@foreach($sells as $sell) 
    {{ $sell->orders }} //for orders.
    {{ $sell->products }} //for product
    @foreach($sell->orders as $order)
        {{ $order->product }} //single product
    @endforeach
@endforeach

是的,您是对的。卖家和买家都是用户。那么如何添加这种关系呢?在产品表中,我只有用户id,没有买家id或卖家id,因为任何用户都可以买卖。@Hasan05我认为您需要更改数据库结构。因为您想从用户帐户(卖家)购买到另一个用户帐户(买家)。如果我没有理解错。不是吗?那么我必须在产品表中添加卖方id和买方id列?不,他们需要在订单表中添加。可以再次阅读整个答案吗?并尝试步骤吗?如果它不能解决您的问题,那么让我知道具体的问题。您面临的问题是什么以及在哪里。是的,您是正确的,卖方和买方都是用户。那么如何解决o是否添加该关系?在产品表中,我只有用户id,没有买方id或卖方id,因为任何用户都可以买卖。@Hasan05我认为您需要更改数据库结构。因为您想从用户帐户(卖方)购买到另一个用户帐户(买方)。如果我没有理解错。不是吗?所以我必须在产品表中添加卖方id和买方id列?不,他们需要在订单表中添加。可以再次阅读整个答案吗?并尝试步骤吗?如果它不能解决您的问题,请让我知道具体的。您面临的问题是什么以及在哪里。
@foreach($sells as $sell) 
    {{ $sell->orders }} //for orders.
    {{ $sell->products }} //for product
    @foreach($sell->orders as $order)
        {{ $order->product }} //single product
    @endforeach
@endforeach