简体   繁体   English

我有 3 个有关系的表。 第一个表具有另一个两个表的外部 id。 我想知道获取数据的确切查询

[英]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. 3 MODELS- 3款-

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');
    }
}
  1. CONTROLLER CODE - CONTROLLER 代码 -
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');

  1. TABLES -表 -

4.1) Product Table - 4.1) 产品表 -

id(int PK) id(int PK) product_name(varchar(100))产品名称(varchar(100)) bank_id(ForeignKey) bank_id(外键) category_id(ForeignKey) category_id(外键)
1 1 p1 p1
2 2 p2 p2

4.2) Bank table- 4.2) 银行表-

id(int PK) id(int PK) bank_name银行名称 bank_logo银行标志
1 1 Bank1银行1 img1图像1
2 2 Bank2银行2 img2 img2

4.3) Product Category Table - 4.3) 产品类别表 -

id(int PK) id(int PK) category_name分类名称
1 1 cat1猫1
2 2 cat2猫2

OUTPUT Should be - OUTPUT 应该是 -

id ID category_name分类名称 bank_name银行名称 product_name产品名称
1 1 cat1猫1 bank1 bank2 bank3银行 1 银行 2 银行 3 p1/p2/p3(bank1), p1/p2(bank2), p1/p2/p3(bank3) p1/p2/p3(bank1), p1/p2(bank2), p1/p2/p3(bank3)
2 2 cat2猫2 bank1 bank2 bank3银行 1 银行 2 银行 3 p1/p2/p3(bank1), p1/p2/p3(bank2), p1/p2/p3(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产品分类 银行类_产品银行_产品

Products产品

id ID product_name产品名称
1 1 p1 p1
2 2 p2 p2

Categories类别

id ID category_name分类名称
1 1 c1 c1
2 2 c2 c2

Banks银行

id ID bank_name银行名称
1 1 b1 b1
2 2 b2 b2

category_product类别_产品

id ID catrgory_id catrgory_id product_id product_id
1 1 1 1 2 2
2 2 2 2 1 1

bank_product银行产品

id ID bank_id银行ID product_id product_id
1 1 1 1 2 2
2 2 2 2 1 1

Models楷模

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 如果你做了命名权就会自己发现它,

Controller Controller

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.

相关问题 我有2个MySql表。 如何在1 sql查询中获取不是所有数据? - I have 2 MySql tables. How to get not all data from them in 1 sql query? SQL查询-我可以查询两个都有ID字段的表并检索这两个字段吗? - SQL query - Can I query 2 tables that both have an ID field and retrieve both fields? 我有两个表第一个表有一些数据另一个表第二个有所有数据现在联接查询空值显示在json响应中 - i have two tables 1st table have some data another table 2nd have all data now join query null value showing in json response 我想从 MySQL 中的 2 个表中按日期排序数据 - I want to have data from 2 tables in MySQL ordered by date 比较两个SQL表,如果它们在公共输出表1值和表2值中有一个ID,我想始终输出表1的值 - Comparing two SQL tables I want to always output a table 1 value if they have an ID in common output table 1 value and table 2 value mysql表有两个来自另一个表的外键 - mysql tables have two foreign keys from another same table 我有三个表,当我从它们中检索数据时,我也想获取表名 - i have three tables and when i retrieve data from them i also want to get the table name too 我需要更新连接两个不同表的表。 此查询合适吗? - I need to update a table joining two different tables. Is this query the appropriate one? 请求 10 表。 表有不同的结构 - Request in 10 tables. Tables have different structures 我有两个表单,都以id作为主键,我希望在第一个表单中输入的id在下一个表单中更新 - I have two forms both with an id as the primary key, I want the id entered in the first form to update in the next form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM