R
R
Ruslan Yanborisov2018-12-02 15:39:52
Laravel
Ruslan Yanborisov, 2018-12-02 15:39:52

How to use foreign database keys on a website?

There are two tables in the database: users and area, in users there are user data, and in area - the names of the districts.
Foreign keys are set up as users.area_id = area.area_id, everything works well in phpmyadmin itself.
On the site I load both tables like this:

$users= DB::select('select * from users');
$areas = DB::select('select * from area');

To display the name of the district, I used a bad construction before:
{{ $areas[$users->area_id]->name }}
I don’t need to explain why it’s bad)
One guy saw what he did like this:
{{ $users->area_id->name }}
Question: How can I do the same, I’m just breaking my head for the second day.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2018-12-02
@Rus_K_o

https://laravel.ru/docs/v5/eloquent-relationships
If you don't know how to speak English

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question