简体   繁体   中英

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-

1.1) PRODUCT 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 -

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 -

class ProductCatgory extends Model
{
    protected $table = 'mudra5_productcategory';
    
     public function mudra5_product()
    {
        return $this->hasMany('App\Models\Product');
    }
}
  1. CONTROLLER CODE -
public function cat()
{
    $alldata = Bank::with(['product'])->first();
    dd($alldata->product[0]->product_name);
}

3.ROUTE -

Route::get('/borrower_profile', 'BorrowerAuth\statementController@cat')->name('mudra5_product');

  1. TABLES -

4.1) Product Table -

id(int PK) product_name(varchar(100)) bank_id(ForeignKey) category_id(ForeignKey)
1 p1
2 p2

4.2) Bank table-

id(int PK) bank_name bank_logo
1 Bank1 img1
2 Bank2 img2

4.3) Product Category Table -

id(int PK) category_name
1 cat1
2 cat2

OUTPUT Should be -

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.

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.

you cannot put hasMany and belongsToMany together. 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.

if you want to guard the relationships you presented than you need 5 tables (not 3)

Products Categories Banks category_product bank_product

Products

id product_name
1 p1
2 p2

Categories

id category_name
1 c1
2 c2

Banks

id bank_name
1 b1
2 b2

category_product

id catrgory_id product_id
1 1 2
2 2 1

bank_product

id bank_id product_id
1 1 2
2 2 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,

Controller

first product:

    public function cat()
    {
        $firstBank= Bank::first();
        dd($firstBank->products()->first()->product_name);
    }

same output as your example? : 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

check this repo for refrence where it has manyToMany relationship between Tags & Posts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM