Answer the question
In order to leave comments, you need to log in
Is it possible in Laravel 5.4 to get (display) title by ID from another column?
Greetings!
Tell me, is it possible in Laravel 5.4 using relations (or in any other way) to get (display) the title by ID from another column?
For example, there are two tables:
[user]
| id | name | country_id |
| 1 | Vasya | 2 |
| 2 | Petya | 1 |
[country]
| id | name |
| 1 | USA |
| 2 | Russia |
Vasya, Russia
Petya, USA
Answer the question
In order to leave comments, you need to log in
The answer turned out to be a bit more complicated:
Additional fields must be specified in the App\User model:
public function country() {
return $this->hasOne('App\Country', 'id', 'country_id');
}
public function user() {
return $this->belongsTo('App\User');
}
$user->country()->name;
you can https://laravel.com/docs/5.4/eloquent-relationships specifically in your example it is "one to one"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question