【问题标题】:Orders placed by a buyer are not showing to sellers Laravel买家下的订单没有显示给卖家 Laravel
【发布时间】:2019-10-28 21:33:43
【问题描述】:

我正在尝试让买家看到的订单被相应的卖家看到,但它没有显示任何内容。

我已经尝试了几个小时来解决这个问题,但我仍然卡住了。

这是我的模型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');
    }
}

这是我的 OrderProduct.php

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

这是我的User.php

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

最后是我在 ProductController

中的 viewOrder 函数
//Orders View Function
public function viewOrders(User $user)
{
    $products = Products_model::where('user_id', '=', $user->id)->get();
    $orders = [];
    foreach($products as $product){
        array_merge($orders, $product->order);
    }
    //dd( $products);
    return view('orders')->with(compact('orders'));
}

我需要每个列出产品的卖家(用户)在其他买家(用户)购买时收到订单。到目前为止它显示 "订单 ID 订单日期 客户姓名 客户城市 客户电话

【问题讨论】:

    标签: php laravel eloquent laravel-5.8


    【解决方案1】:

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

    所以为卖家添加一个关系。

    注意:我假设买家和卖家都是User

    您必须为买方和卖方添加关系(不要忘记将它们也添加到数据库中。这意味着您需要在orders table 上添加buyer_idseller_id 字段)。

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Order extends Model
    {
         //protected $table = 'orders';
         protected $fillable =  [
            'buyer_id', 'seller_id', 'shipping_email', 'shipping_name', 'shipping_city',    'shipping_phone', 'billing_subtotal', 'billing_total',
         ];    
    
    
         public function products()
         {
             return $this->belongsToMany('App\Products_model')->withPivot('quantity');
         }
    
         public function orders(){
              return $this->hasMany('App\OrderProduct', 'order_id');
         }
         public function buyer()
         {
             return $this->belongsTo(User::class, 'id', 'buyer_id');
         }
    
         public function seller()
         {
             return $this->belongsTo(User::class, 'id', 'seller_id');
         }
      }
    

    将这两个关系添加到 User 模型中。这样您就可以轻松地调用它们。

     public function buys() {
        $this->hasMany(Order::class, 'buyer_id', 'id');
     }
     public function sells() {
         $this->hasMany(Order::class, 'seller_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
    

    【讨论】:

    • 是的,你说得对,卖家和买家都是用户。那么我该如何添加这种关系呢?
    • 在产品表中我只有user_id,我没有buyer_id 或seller_id,因为任何用户都可以买卖。 @hasan05
    • 我认为您需要更改数据库结构。因为您想从 一个用户帐户(卖方)购买另一个用户帐户(买方)。 如果我没有理解错误。我不是吗?
    • 所以我必须在产品表中添加seller_id 和buyer_id 列?
    • 不,他们需要添加到订单表中。可以再次阅读整个答案吗?并尝试这些步骤?如果它不能解决您的问题,请让我知道具体。您面临的问题和问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多