mardi 9 juillet 2019

Confused with Laravel's hasOne Eloquent Relationships

I have a new Laravel 5.8 application. I started playing with the Eloquent ORM and its relationships.

There is a problem right away that I encountered.

I have the following tables. (this is just an example, for testing reasons, not going to be an actual application)

Login table:
--------------------------
| id | user    | data_id |
--------------------------
| 1  | admin   | 1       |
| 2  | admin   | 2       |
| 3  | admin   | 3       |
--------------------------

Data table:
--------------
| id | ip_id |
--------------
| 1  | 1     |
| 2  | 2     |
| 3  | 3     |
--------------

IP table:
----------------------
| id | ip            |
----------------------
| 1  | 192.168.1.1   |
| 2  | 192.168.1.2   |
| 3  | 192.168.1.3   |
----------------------

What I wanted is to get the IP belonging to the actual login.

So I added a hasOne relationship to the Login table that has a foreign key for the Data table:

public function data()
{
    return $this->hasOne('App\Models\Data');
}

Then I added a hasOne relationship to the Data table that has a foreign key for the IP table:

public function ip()
{
    return $this->hasOne('App\Models\Ip');
}

Once I was done, I wanted to retrieve the IP address for the first record of the Login table:

Login::find(1)->data()->ip()->get();

But I get this error:

Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::ip()

What am I missing here and how can I get the IP of that login in the correct way? Do I need a belongsTo somewhere?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire