繁体   English   中英

如何在Laravel中检索和显示关系数据?

[英]How to retrieve and display relationship data in Laravel?

我在数据库中有三个表。 order_product orders, productsorder_product表。 这就是他们在我的phpmyAdmin https://imgur.com/a/Ud9e2Hh我希望卖家在他们的视图中(见dashboard )看到买家下的订单。 此外,买家可以在自己的视图中查看其订单。

这是我在模型中的关系

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 isSeller() {
    //   return $this->seller;
    //}

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

Order.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    //protected $table = 'orders';
    protected $fillable =  [
        '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');
    }
}

卖方功能

// Seller Orders 
public function viewOrders(User $user)
{

    // $products = Products_model::where('seller_id', '=', $user->id)->get();
    // all sells
    $sells = $user->orderFromSellers();
    dd($sells);
    return view('orders')->with(compact('sells'));
}

买方职能

//Buyer Orders
public function myOrders()
{
    $cart = session();
    $orders = Auth::user()->orders;
    $orders->transform(function($order, $key) {
        dd($orders);
        $order->cart = unserialize($order->cart);
        return $order;
    });
    return view('myOrders', ['orders' => $orders]);
}

现在它什么也没显示。 任何有关如何解决此问题的帮助将不胜感激。

产品_型号

protected $table='products';
protected $primaryKey='id';
protected $fillable=['seller_id','pro_name','pro_price','pro_info','image','stock','category_id '];

尝试通过更改两个功能。

public function orderFromBuyers()
{
    return $this->hasManyThrough(Products_model::class, OrderProduct::class,  'buyer_id', 'product_id');
}
public function orderFromSellers()
{
    return $this->hasManyThrough(Products_model::class, OrderProduct::class, 'seller_id', 'product_id');
}

否则两者会更好。 将这两个功能添加到用户模型。

public function allOrderFromBuyers()
{
    return $this->hasMany(OrderProduct::class, 'buyer_id');
}

public function allOrderFromSellers()
{
    return $this->hasMany(OrderProduct::class, 'seller_id');
}

然后将其更改为

// Seller Orders 
public function viewOrders(User $user)
{
    // all sells
    $sells = $user->allOrderFromSellers();
    dd($sells);
    return view('orders')->with(compact('sells'));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM