Hello. I have models and tables Complex, Floors with the following structures:

complex - complex_id, number, descriptiom floors - floor_id, complex_id, number, description

In the Complex model, I make a one-to-many connection (one complex can have several floors):

public function floors(){ return $this->hasMany('App\Floors', 'complex_id'); } 

In the Floors model, I do the opposite relationship:

 public function complex(){ return $this->belongsTo('App\Complex', 'complex_id'); } 

Trying to get all the floors of the first complex:

 Complex::first()->floors(); 

The result is empty. What could be wrong?

  • and if so Complex::first()->floors ? - Zippbl4
  • Same as empty array. The data in the tables is checked - user137
  • Specifying the local key also had no effect - user137
  • one
    $ this-> hasMany ('App \ Floors', 'complex_id', 'complex_id'); ? - Zippbl4
  • Yes, $ this-> hasMany ('App \ Floors', 'complex_id', 'complex_id') works, thanks a lot. - user137

0