S
S
Sparkqy2019-05-06 22:19:04
Laravel
Sparkqy, 2019-05-06 22:19:04

How to get another one from the received collection from hasMany(), but for a different table?

Good day! Started learning Laravel (v5.8).
I have 3 tables: 1) audiences 2) devices 3) device_audiences
1) 5cd088315a1c6415333251.png
2) 5cd08853dc53e580689683.png
3) 5cd08873192d5093442691.png
I need to display a list of devices that are in it on the audience page. I do in the Audience model:

public function devices()
{
    $this->hasMany(DeviceAudience::class);
}

- I get a collection from the table of connections between the audience and devices, but I need to return another one from this collection, already specifically according to the table of devices. How it's done? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-05-06
@SparkyToster

In the Audience model

public function devices() {
      return $this->belongsToMany('App\Models\Device', 
                              'device_audiences', 
                              'audience_id', 
                              'device_id')->withPivot('amount');
}

You get auditorium devices in the controller.
Others are similar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question