[英]I have 3 tables with relation. The first table have foreign id of another both tables. I want to know the exact query of fetching the data
1.1) PRODUCT MODEL - 1.1) 产品 MODEL -
class Product extends Model
{
protected $table = 'product_table(TableName)';
public function banks()
{
return $this->belongsToMany(Bank::class, 'bank_table', 'bank_id(ForeignKey)', 'id(PrimaryKey)');
}
public function product_category()
{
return $this->belongsToMany(ProductCategory::class, 'productcategory_table', 'productcategory_id(ForeignKey)', 'id(PrimaryKey)');
}
}
1.2) BANK MODEL - 1.2) 银行 MODEL -
class Bank extends Model
{
protected $table = 'bank_table(TableName)';
public function product()
{
return $this->hasMany('App\Models\Product', 'id(PrimaryKey)', 'bank_id(ForeignKey)');
}
}
1.3) PRODUCT CATEGORY MODEL - 1.3) 产品类别 MODEL -
class ProductCatgory extends Model
{
protected $table = 'mudra5_productcategory';
public function mudra5_product()
{
return $this->hasMany('App\Models\Product');
}
}
public function cat()
{
$alldata = Bank::with(['product'])->first();
dd($alldata->product[0]->product_name);
}
3.ROUTE - 3.路线 -
Route::get('/borrower_profile', 'BorrowerAuth\statementController@cat')->name('mudra5_product');
4.1) Product Table - 4.1) 产品表 -
id(int PK) ![]() |
product_name(varchar(100))![]() |
bank_id(ForeignKey) ![]() |
category_id(ForeignKey) ![]() |
---|---|---|---|
1 ![]() |
p1 ![]() |
||
2 ![]() |
p2 ![]() |
||
4.2) Bank table- 4.2) 银行表-
id(int PK) ![]() |
bank_name![]() |
bank_logo![]() |
---|---|---|
1 ![]() |
Bank1![]() |
img1![]() |
2 ![]() |
Bank2![]() |
img2 ![]() |
4.3) Product Category Table - 4.3) 产品类别表 -
id(int PK) ![]() |
category_name![]() |
---|---|
1 ![]() |
cat1![]() |
2 ![]() |
cat2![]() |
OUTPUT Should be - OUTPUT 应该是 -
id ![]() |
category_name![]() |
bank_name![]() |
product_name![]() |
|
---|---|---|---|---|
1 ![]() |
cat1![]() |
bank1 bank2 bank3![]() |
p1/p2/p3(bank1), p1/p2(bank2), p1/p2/p3(bank3) ![]() |
|
2 ![]() |
cat2![]() |
bank1 bank2 bank3![]() |
p1/p2/p3(bank1), p1/p2/p3(bank2), p1/p2/p3(bank3) ![]() |
|
if you put the foreign key then the relationship is HasMany-belongsTo (1-N), for example if each product had one category then we put the foreign key of category_id in the product table since it can be only one.如果放置外键,则关系为 HasMany-belongsTo (1-N),例如,如果每个产品都有一个类别,那么我们将 category_id 的外键放在产品表中,因为它只能是一个。
in your case you have used belongsToMany not belongsTo thus a product can have many categories but you also put a foreign key in the table which dosnt go along with the belongsToMany.在您的情况下,您使用了belongsToMany而不是belongsTo,因此一个产品可以有很多类别,但您还在表中放置了一个外键,它与belongsToMany一起dosnt go。
you cannot put hasMany and belongsToMany together.你不能把 hasMany 和 belongsToMany 放在一起。 choose one of them.
选择其中之一。
a belongsToMany is an eloquent relationship for NN relationships and they require a pivot table no table can hold the other one _id so you make a picot table that has both ids and this table will handle the relationships queries traffic.一个belongsToMany 是一个用于NN 关系的eloquent 关系,它们需要一个pivot 表,没有表可以保存另一个_id,因此您创建一个具有两个id 的picot 表,该表将处理关系查询流量。
if you want to guard the relationships you presented than you need 5 tables (not 3)如果你想保护你呈现的关系而不是你需要 5 个表(不是 3 个)
Products Categories Banks category_product bank_product产品分类 银行类_产品银行_产品
id ![]() |
product_name![]() |
---|---|
1 ![]() |
p1 ![]() |
2 ![]() |
p2 ![]() |
id ![]() |
category_name![]() |
---|---|
1 ![]() |
c1 ![]() |
2 ![]() |
c2 ![]() |
id ![]() |
bank_name![]() |
---|---|
1 ![]() |
b1 ![]() |
2 ![]() |
b2 ![]() |
id ![]() |
catrgory_id ![]() |
product_id ![]() |
---|---|---|
1 ![]() |
1 ![]() |
2 ![]() |
2 ![]() |
2 ![]() |
1 ![]() |
id ![]() |
bank_id![]() |
product_id ![]() |
---|---|---|
1 ![]() |
1 ![]() |
2 ![]() |
2 ![]() |
2 ![]() |
1 ![]() |
i you go by the docs you dont have to specify the table name of foreign key name laravel will discover it itself if you did the naming right,我你 go 通过文档你不必指定外键名的表名 laravel 如果你做了命名权就会自己发现它,
first product:第一个产品:
public function cat()
{
$firstBank= Bank::first();
dd($firstBank->products()->first()->product_name);
}
same output as your example?与您的示例相同的 output 吗? : each category with its banks and its products
:每个类别及其银行及其产品
public function cat()
{
dd(Catgeory::with('banks' , 'products')->get());
}
please check the official documentation of eloquent ORM here it explains lots of thing with examples请查看 eloquent ORM 的官方文档这里它通过示例解释了很多事情
check this repo for refrence where it has manyToMany relationship between Tags & Posts检查此 repo以获取标签和帖子之间的多对多关系的参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.