Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中检索和显示关系数据?_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

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

Php 如何在Laravel中检索和显示关系数据?,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,数据库中有三个表订单、产品,以及订单与产品表。这就是它们在myphpmyAdmin中的样子 我希望卖家能在他们的视图中看到买家下的订单(仪表板)。此外,买家还可以在自己的视图中查看订单 以下是我在模型中的关系 User.php <?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundati

数据库中有三个表<代码>订单、产品,以及
订单与产品
表。这就是它们在my
phpmyAdmin中的样子
我希望卖家能在他们的视图中看到买家下的订单(
仪表板
)。此外,买家还可以在自己的视图中查看订单

以下是我在模型中的关系

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

尝试更改两个函数

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


您好@joh我能看看您的产品\模型数据库字段吗?我已经用产品\模型@hasan05更新了问题,没有主字段吗?比如id?@hasan05没有主字段是什么意思?@hasan05如果你看这个链接,它会显示products表中的所有字段Hello@joh我能看到你的products\u model数据库字段吗?我已经用products\u model@hasan05更新了问题没有主字段吗?比如id?@hasan05没有主字段是什么意思?@hasan05如果你看这个链接,它会显示产品表中的所有字段哦,对不起,我的错。现在试试。你有什么更新吗?因为我仍然会犯同样的错误。嘿@Hasan05,你能帮我看看为什么它没有显示出来吗?我可以,但是,这将是我的方式的一个重大改变,也会有更多的时间。多尝试12小时。如果你不能,那就敲我。我会帮你调试的。这是第二天试着看看有什么问题。如果你能抽出一些时间来帮忙,那就太好了。@hasan05哦,对不起,我的错。现在试试。你有什么更新吗?因为我仍然会犯同样的错误。嘿@Hasan05,你能帮我看看为什么它没有显示出来吗?我可以,但是,这将是我的方式的一个重大改变,也会有更多的时间。多尝试12小时。如果你不能,那就敲我。我会帮你调试的。这是第二天试着看看有什么问题。“如果你能抽出一些时间来帮忙,那就太好了。”Hasan05