How to create relationship between 3 models in laravel?

Related searches

SQL scheme:

bulletins

  id increment

deals

  id increment
  seller_id
  buyer_id

deals_items - items = bulletins

  id increment
  title
  desc
  bulletin_id
  deal_id

How can I get deal row by bulletin id? In raw SQL it looks like:

select `deals`.* from `deals` inner join `deals_items` on `deals_items`.`deal_id` = `deals`.`id` where `deals_items`.`bulletin_id` = 10572

I tried:

public function deals()
{
    return $this->hasManyThrough(DealItem::class,Deal::class, 'bulletin_id','dealid','id');
}

But it seems a wrong way. Can't find right way in laravel doc about relation.

@HCK shows right way.

but when I doing $bulletin->deals() in blade template I got empty collection of deals.

When just $bulletin->deal - all is fine, we have collection of deals.

I using protected $with = ['deals'] in my bulletin model, but what is different call method or property? Why with method empty result?

@Amarnasan was close, but the order of the foreign keys was wrong. Try this:

Deal.php

public function bulletins()
{
    return $this
        ->belongsToMany(Bulletin::class, 'deals_items', 'deal_id', 'bulletin_id')
        ->withPivot('title','desc');
}

Bulletin.php

public function deals()
{
    return $this
        ->belongsToMany(Deal::class, 'deals_items', 'bulletin_id', 'deal_id')
        ->withPivot('title','desc');
}

From the docs:

As mentioned previously, to determine the table name of the relationship's joining table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method:

return $this->belongsToMany('App\Role', 'role_user');

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:

return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');

Update

When you access the relationship as a method: $bulletin->deals() you are accessing the relationship itself. This will return an instance of \Illuminate\Database\Eloquent\Relations\BelongsToMany (in your case). Here the query is not executed yet, so you could keep adding constrains to your query, for example:

$bulletin
    ->deals()
    ->where('seller_id', 45) // <---
    ->skip(5) // <---
    -> ... (And so on)

When you access it as a dynamic property, you are already executing the query, so this will return a Collection instance. Is the same as calling the relationship as a method and then attach the ->get() at the end, so this two are equivalent:

$bulletin->deals()->get()
// equals to:
$bulletin->deals

Check this other answer, it answers your question.

How to setup relationship between 3 Model in Laravel?, I would suggest addiung foreign keys to classrooms and faculties tables. Afterwards you need to assign add relations between each model,� Use Many to Many Relationship To do so you will create 3 tables: Laravel Relationships: 3 Models. 1. Multilevel relationship whereHas on eloquent Model in Laravel. 0.

DealClass:

public function bulletins()
    return $this->belongsToMany('App\Bulletin', 'deals_items ', 'bulletin_id', 'deal_id')->withPivot('title','desc');
}

BulletinClass:

public function deals()
    return $this->belongsToMany('App\Deal', 'deals_items ', 'deal_id', 'bulletin_id')->withPivot('title','desc');
}

Relation between 3 tables?, I have 3 tables (also 3 models), User, Company and UserCompany, (images here: I get company id, how can I create now relation (which type) to get company name? https://laravel.com/docs/5.3/eloquent-relationships#many-to- many. Step 3: Make models for all these new three tables. Type the following command. php artisan make:model Item php artisan make:model Order php artisan make:model Invoice One To Many Relationships. A “one-to-many” relationship is generally used to define relationships where a single or one model owns any amount of the other models.

deals model -

public function bulletins()
    return $this->belongsToMany(Bulletin::class, 'deals_items ', 'bulletin_id', 'deal_id');
}

bulletin model:-

public function deals()
{
    return $this
        ->belongsToMany(Deal::class, 'deals_items',  'deal_id', 'bulletin_id',);
}

relationship between 3 tables with Eloquent, relationship between 3 tables with Eloquent class Estimate extends Model { protected $table = 'estimate'; public function order() { return� For inverse one to many relationship, Laravel look into brands table with the ID from brand_id column and return the matching record. Creating a One To Many Relationship. To create a relationship between two models or associating them, we can first create a child object and then save it using the parent object.

Eloquent relationships, Eloquent determines the foreign key of the relationship based on the model name. In this case First, let's examine the table structure required to build this relationship: select * from books select * from authors where id in (1, 2, 3, 4, 5, .. .)� Ok, we have DB tables and migrations, now let’s create models for them. The main part here is to assign a many-to-many relationship – it can be done from either of “main” tables models. So, option 1: app/Shop.php: class Shop extends Model { /** * The products that belong to the shop.

Code Bright: Eloquent Relationships, Learn how to create relationships between your Laravel PHP Eloquent models. 1 | Code Sexy | 2 | | 2 | Code Dutch | 3 | | 3 | Code Bright | 1 | | 4 | Code Happy� composer create-project laravel/laravel ManyToMany --prefer-dist. Once you have a fresh installation of Laravel, go ahead a setup your database credentials in .env file. Creating Model and Migrations. Firstly, we will create two models called Product and Category along with their migrations. Run below command to generate models and migrations.

To quickly scaffold your models together with a migration, I would advise to create a new Laravel application (a “dummy application” just for the creation of models / migrations / etc.) and use the php artisan make:model -m command and copy the model to the package’s src/Models directory and using the proper namespace.

Comments
  • in which model you are creating this relationship. and also show response of data.
  • in bulletins model, response is error cause laravel generating wrong query. I played with keys in hasManyThrough and have't success - always wrong query. Only manually edited as I show works as I want.
  • SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name 'deals_items ' (SQL: select own.*, deals_items .deal_id as pivot_deal_id, deals_items .bulletin_id as pivot_bulletin_id, deals_items .title as pivot_title, deals_items .description as pivot_description from own inner join deals_items on own.id = deals_items .bulletin_id where deals_items .deal_id in (3)) - own is a bulletins table name
  • also I can't understand why we need relation deals->bulletins does it uses by relation in bulletins->deals? I think no.
  • @rst630 It isn't necessary, it's just good practice to define both relationships, but yeah you can skip it.
  • @rst630 my bad. I forgot to delete it, I fixed though.
  • great now it works, but can you explain me one moment dd($bulletin->deals) - all ok, dd($bulletin->deals()) - in blade - have empty relation with Deal. I using $with=... in model but when we calling method not property it's just rerun query - why no Deal in this case?
  • got empty Deal collection in this case