我正在尝试向卖家显示订单,但是买家也可以看到他们的订单。我已经创建了所有必要的关系,并且数据在数据库中。我陷入尝试从数据库查询此数据,以便我可以显示它。我有这些表的订单表,order_product表。它们是https://imgur.com/a/Ud9e2Hh
的样子我尝试了以下功能,但是解决问题仍然不走运。 如果您需要更多信息,请发表评论,我会提供。
这是我的功能
// Seller Orders
public function viewOrders(User $user)
{
// all sells
$sells = $user->allOrderFromSellers();
dd($sells);// this returns empty array
return view('orders')->with(compact('sells'));
}
//Buyer Orders
public function myOrders(User $user)
{
return view('myOrders', compact('user','orders'));
dd($orders);
}
这是模型。
order_product.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);
}
}
这是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');
}
public function allOrderFromBuyers()
{
return $this->hasMany(OrderProduct::class, 'buyer_id');
}
public function allOrderFromSellers()
{
return $this->hasMany(OrderProduct::class, 'seller_id');
}
}
上面的函数给了我这个结果:
"Collection {#281 ▼
#items: []
}"
请帮助我,我花了很多时间,但是没有任何作用。
答案 0 :(得分:0)
关系不正确 您需要使用很多对很多并且具有许多直通关系,因为订单有很多产品,而产品有很多订单。 https://laravel.com/docs/5.8/eloquent-relationships#many-to-many https://laravel.com/docs/5.8/eloquent-relationships#has-many-through 不要为数据透视表创建模型和关系,例如OrderProduct 那是不对的 在这种情况下,order_product是数据透视表
所以这是正确的模型
对于订单模型
public function products()
{
return $this->belongsToMany('App\Product', 'order_product');
}
对于产品型号
public function orders()
{
return $this->belongsToMany('App\Order', 'order_product');
}
之后,您可以处理其余的事情
$order = Order::with('products')->findOrFail($id);
$products = $order->products