D
D
den-masonov2017-01-31 15:30:53
ORM
den-masonov, 2017-01-31 15:30:53

Communication "to many through". How to make a query to get the required data?

Hello everyone!)
My problem is this: there are three tables:

class Area extends Model
{
    public function institutions()
    {
        return $this->hasManyThrough('App\Institution', 'App\City');
    }
}

class City extends Model
{
  public function institutions()
    {
        return $this->hasMany('App\Institution');
    }
}

class Institution extends Model
{

  public function area() {
    return $this->belongsTo('App\Area');
  }

}

I need to get data from institutions
. I don't know how to make a request, I read everything I managed to google, but I'm missing something.
$institutions = Area::find(1)->cities->institutions doesn't work
$institutions = Area::find(1)->cities()->where('id', '=', 1)->institutions I tried that too
I tried it like this:
$inst = Area::with('cities')->get();

        foreach ($cities as $city) {
          $city->institutions->name;
        }

Gives an error message:
RelationNotFoundException in RelationNotFoundException.php line 20:
Call to undefined relationship [cities] on model [App\Area].

I tried to understand what they were telling me, but with my knowledge of English and knowledge of Laravel, I did not come to much success.
In general, I need a collection of institutions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mokhirjon Naimov, 2017-03-17
@den-masonov

Looks like you didn't define a method for communication cities()in the modelApp\Area

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question