dongzhizhai4070 2019-06-17 11:46 采纳率: 0%
浏览 90

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

I have three tables in the database. orders, products, and order_product table. this is how they look like in my phpmyAdmin https://imgur.com/a/Ud9e2Hh I would like sellers to see orders placed by buyers in their view (dashboard). Also, buyers to be able to see their orders in their view.

Here are my relationships in models

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 Function

// 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 Function

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

Right now it shows nothing. Any help on how to solve this will be appreciated.

Products_model

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

1条回答 默认 最新

  • drju37335 2019-06-17 19:44
    关注

    Try by changing two function.

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

    Or the two will be more better. Add these two functions to User model.

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

    Then change this to,

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题