Php 拉威尔怎么能得到这种关系
考虑一个Php 拉威尔怎么能得到这种关系,php,laravel,Php,Laravel,考虑一个数字商店 定义: Buyer->all buyer Products->all products Downloads->store those products that buyer bought ----------------------- |id | name | seller_id | ----------------------- | 1 | bmw | 1 | ----------------------- | 2 | benz |
数字商店
定义:
Buyer->all buyer
Products->all products
Downloads->store those products that buyer bought
-----------------------
|id | name | seller_id |
-----------------------
| 1 | bmw | 1 |
-----------------------
| 2 | benz | 1 |
-----------------------
| 2 | reno | 2 |
-----------------------
------------------------------
|id | product_id | buyer_id |
------------------------------
| 1 | 1 | 1 |
------------------------------
| 2 | 1 | 2 |
------------------------------
| 3 | 2 | 22 |
------------------------------
------------------------------
|id | name | email |
------------------------------
| 1 | john | @ |
------------------------------
| 2 | mike | @ |
------------------------------
| 3 | dave | @ |
------------------------------
买家
可以购买产品
并将其存储在下载中
,现在我想向买家显示下载列表
ProductController.php
public function buyerproducts()
{
$user = auth()->guard('buyer')->user();
if ($user) {
$files = Product::where('deleted', 0)
->where('deleted', 0)
->with('files', 'province:id,name', 'city:id,name')
->get();
// and here I got a loop.. to add some extra data
return response()->json(['data' => $files], 200);
} else {
return response()->json(['success' => 'no content'], 204);
}
}
function files()
{
return $this->hasManyThrough('App\Download', 'App\Buyer', 'id', 'product_id', 'buyer_id', 'id');
}
Product.php
public function buyerproducts()
{
$user = auth()->guard('buyer')->user();
if ($user) {
$files = Product::where('deleted', 0)
->where('deleted', 0)
->with('files', 'province:id,name', 'city:id,name')
->get();
// and here I got a loop.. to add some extra data
return response()->json(['data' => $files], 200);
} else {
return response()->json(['success' => 'no content'], 204);
}
}
function files()
{
return $this->hasManyThrough('App\Download', 'App\Buyer', 'id', 'product_id', 'buyer_id', 'id');
}
但它会返回所有数据,而不是买家购买的数据。有什么想法吗?
请注意,我必须在product controller中获取此数据,而不是下载
产品:
Buyer->all buyer
Products->all products
Downloads->store those products that buyer bought
-----------------------
|id | name | seller_id |
-----------------------
| 1 | bmw | 1 |
-----------------------
| 2 | benz | 1 |
-----------------------
| 2 | reno | 2 |
-----------------------
------------------------------
|id | product_id | buyer_id |
------------------------------
| 1 | 1 | 1 |
------------------------------
| 2 | 1 | 2 |
------------------------------
| 3 | 2 | 22 |
------------------------------
------------------------------
|id | name | email |
------------------------------
| 1 | john | @ |
------------------------------
| 2 | mike | @ |
------------------------------
| 3 | dave | @ |
------------------------------
下载:
Buyer->all buyer
Products->all products
Downloads->store those products that buyer bought
-----------------------
|id | name | seller_id |
-----------------------
| 1 | bmw | 1 |
-----------------------
| 2 | benz | 1 |
-----------------------
| 2 | reno | 2 |
-----------------------
------------------------------
|id | product_id | buyer_id |
------------------------------
| 1 | 1 | 1 |
------------------------------
| 2 | 1 | 2 |
------------------------------
| 3 | 2 | 22 |
------------------------------
------------------------------
|id | name | email |
------------------------------
| 1 | john | @ |
------------------------------
| 2 | mike | @ |
------------------------------
| 3 | dave | @ |
------------------------------
买方:
Buyer->all buyer
Products->all products
Downloads->store those products that buyer bought
-----------------------
|id | name | seller_id |
-----------------------
| 1 | bmw | 1 |
-----------------------
| 2 | benz | 1 |
-----------------------
| 2 | reno | 2 |
-----------------------
------------------------------
|id | product_id | buyer_id |
------------------------------
| 1 | 1 | 1 |
------------------------------
| 2 | 1 | 2 |
------------------------------
| 3 | 2 | 22 |
------------------------------
------------------------------
|id | name | email |
------------------------------
| 1 | john | @ |
------------------------------
| 2 | mike | @ |
------------------------------
| 3 | dave | @ |
------------------------------
HasManyThrough
关系将经历2个hasMany
关系,但是,查看表定义,第一个关系是hasMany
,第二个关系是belongsTo
。由于两个键都与不同表中的一行相关(都是belongsTo
),因此我们可以改为创建belongstomy
关系,并将downloads
表作为轴心
有两种不同的方法可以解决这个问题 首先,我建议在
买家
和产品
之间建立关系(您是否还没有这样做):
产品
public function buyers()
{
return $this->belongsToMany(Buyer::class, 'downloads')->withTimestamps();
}
买方
public function products()
{
return $this->belongsToMany(Product::class, 'downloads')->withTimestamps();
}
然后,在控制器方法中,您可以保留相同的查询并使用: 或或者,您不能直接从
$user
(买方)查询产品:
你为什么不把产品作为买方与客体的关系呢 因此,您可以在Buyer.php中定义产品:
function products()
{
return $this->hasManyThrough('App\Download', 'App\Product', 'id', 'buyer_id', 'product_id', 'id');
}
在控制器中,您可以像这样调用它:
$buyer->with([
'products.province:id,name',
'products.city:id,name'
])
->whereHas('products', function($query){
$query->where('deleted', 0)
})
->get()
然后您可以使用
returnresponse()->json(['data'=>$buyer->products],200)代码>请您显示这些表的定义/迁移。@Rwd我添加了表视图,希望它能让这个问题变得清晰谢谢,回答得真棒。它返回的权利,事情,只是一个小问题,我可以包括卖方数据在这个结果?