Laravel 获取关系 return 错误数据
Laravel get relations return wrong data
我有四个table,seller
,products,
和buyers
,downloads
,逻辑是:seller can make products
买家可以购买 products
并存储在 downloads
table。例如:
卖家:
---------------------
|id | name | phone |
---------------------
| 1 | mike | 989898 |
---------------------
| 2 | joe | 989898 |
---------------------
产品:
---------------------
|id | name | user_id|
---------------------
| 1 | benz | 1 |
---------------------
| 2 | bmw | 1 |
---------------------
买家:
----------------------
|id | name | phone |
----------------------
| 1 | carlos | 989898 |
----------------------
| 2 | nina | 989898 |
----------------------
下载次数:
-----------------------------
|id | product_id | buyer_id |
-----------------------------
| 1 | 1 | 2 |
-----------------------------
| 2 | 2 | 2 |
-----------------------------
所以假设买家购买了 id
2 的产品,我想获取产品 (id 2) 和卖家数据 (id 1),另一方面,nina
购买了 benz
和bmw
,这些商品属于mike,所以我想在buyerController中获取商品数据和卖家数据。
class Buyer extends Authenticatable
{
...
public function downloads(){
return $this->hasMany('App\Download','buyer_id');
}
public function products(){
return $this->hasManyThrough('App\Product','App\Seller', 'id', 'user_id');
}
}
在控制器中:
public function files()
{
$buyer = Buyer::find($id);
$files = Buyer::with('downloads', 'products')->get();
return $files;
}
它没有任何错误,但是 return 错误的数据,例如买家不购买的产品,也不是 return 卖家数据,它只是 return "laravel_through_key": 1
.第一个功能运行良好,但第二个功能 hasManyThrough
无法正常工作。
我是Laravel的新手,特别关系。
我做错了什么?
根据你的解释,Buyer
有直接关系downloads
和间接关系products
,但后者是通过downloads
,而不是sellers
。因此,它应该是:
class Buyer extends Authenticatable
{
//...
public function downloads()
{
return $this->hasMany(Download::class);
}
public function products()
{
return $this->hasManyThrough(Product::class, Download::class,
'buyer_id', 'id', 'id', 'product_id');
}
}
不要忘记为其他模型写关系:
class Product extends Model
{
function seller()
{
return $this->belongsTo(Seller::class, 'user_id');
}
function downloads()
{
return $this->hasMany(Download::class);
}
}
class Download extends Model
{
function product()
{
return $this->belongsTo(Product::class);
}
function buyer()
{
return $this->belongsTo(Buyer::class);
}
}
现在你可以使用它的力量了:
1。获取买家产品:
>>> App\Buyer::find(2)->products
=> Illuminate\Database\Eloquent\Collection {#3018
all: [
App\Product {#3022
id: 1,
name: "benz",
user_id: 1,
laravel_through_key: 2,
},
App\Product {#3019
id: 2,
name: "bmw",
user_id: 1,
laravel_through_key: 2,
},
],
}
2。相同,但有卖家信息
>>> App\Buyer::find(2)->products()->with('seller')->get()
=> Illuminate\Database\Eloquent\Collection {#3026
all: [
App\Product {#3034
id: 1,
name: "benz",
user_id: 1,
laravel_through_key: 2,
seller: App\Seller {#3038
id: 1,
name: "mike",
phone: "989898",
},
},
App\Product {#3031
id: 2,
name: "bmw",
user_id: 1,
laravel_through_key: 2,
seller: App\Seller {#3038},
},
],
}
我有四个table,seller
,products,
和buyers
,downloads
,逻辑是:seller can make products
买家可以购买 products
并存储在 downloads
table。例如:
卖家:
---------------------
|id | name | phone |
---------------------
| 1 | mike | 989898 |
---------------------
| 2 | joe | 989898 |
---------------------
产品:
---------------------
|id | name | user_id|
---------------------
| 1 | benz | 1 |
---------------------
| 2 | bmw | 1 |
---------------------
买家:
----------------------
|id | name | phone |
----------------------
| 1 | carlos | 989898 |
----------------------
| 2 | nina | 989898 |
----------------------
下载次数:
-----------------------------
|id | product_id | buyer_id |
-----------------------------
| 1 | 1 | 2 |
-----------------------------
| 2 | 2 | 2 |
-----------------------------
所以假设买家购买了 id
2 的产品,我想获取产品 (id 2) 和卖家数据 (id 1),另一方面,nina
购买了 benz
和bmw
,这些商品属于mike,所以我想在buyerController中获取商品数据和卖家数据。
class Buyer extends Authenticatable
{
...
public function downloads(){
return $this->hasMany('App\Download','buyer_id');
}
public function products(){
return $this->hasManyThrough('App\Product','App\Seller', 'id', 'user_id');
}
}
在控制器中:
public function files()
{
$buyer = Buyer::find($id);
$files = Buyer::with('downloads', 'products')->get();
return $files;
}
它没有任何错误,但是 return 错误的数据,例如买家不购买的产品,也不是 return 卖家数据,它只是 return "laravel_through_key": 1
.第一个功能运行良好,但第二个功能 hasManyThrough
无法正常工作。
我是Laravel的新手,特别关系。 我做错了什么?
根据你的解释,Buyer
有直接关系downloads
和间接关系products
,但后者是通过downloads
,而不是sellers
。因此,它应该是:
class Buyer extends Authenticatable
{
//...
public function downloads()
{
return $this->hasMany(Download::class);
}
public function products()
{
return $this->hasManyThrough(Product::class, Download::class,
'buyer_id', 'id', 'id', 'product_id');
}
}
不要忘记为其他模型写关系:
class Product extends Model
{
function seller()
{
return $this->belongsTo(Seller::class, 'user_id');
}
function downloads()
{
return $this->hasMany(Download::class);
}
}
class Download extends Model
{
function product()
{
return $this->belongsTo(Product::class);
}
function buyer()
{
return $this->belongsTo(Buyer::class);
}
}
现在你可以使用它的力量了:
1。获取买家产品:
>>> App\Buyer::find(2)->products
=> Illuminate\Database\Eloquent\Collection {#3018
all: [
App\Product {#3022
id: 1,
name: "benz",
user_id: 1,
laravel_through_key: 2,
},
App\Product {#3019
id: 2,
name: "bmw",
user_id: 1,
laravel_through_key: 2,
},
],
}
2。相同,但有卖家信息
>>> App\Buyer::find(2)->products()->with('seller')->get()
=> Illuminate\Database\Eloquent\Collection {#3026
all: [
App\Product {#3034
id: 1,
name: "benz",
user_id: 1,
laravel_through_key: 2,
seller: App\Seller {#3038
id: 1,
name: "mike",
phone: "989898",
},
},
App\Product {#3031
id: 2,
name: "bmw",
user_id: 1,
laravel_through_key: 2,
seller: App\Seller {#3038},
},
],
}