0
I am with project Laravel 6.0 and have the entity User and address, where user has 1 address, as well as an address belongs to 1 user (bidirectional).
The problem is that list all addresses and in the list have the column of the user name, even with you, but it does not seem to me the best way, so I would like to help to know the best way. Follows development:
Model User and Adress:
User
public function address(){
return $this->hasOne(Adress::class, 'user', 'id');
}
Adress
public function user(){
return $this->belongsTo(User::class, 'user', 'id');
}
Web.php:: Route:
Route::get('enderecos', 'AdressController@index')->name('adress.list');
Adresscontroller:: Index method to list
public function index()
{
$users = User::all();
$adresses = $this->adress->paginate(5);
return view('adress.index', compact('adresses', 'users'));
}
index.blade.php:: View listing
<table class="table" id="formCad">
<tr>
<td>Id</td>
<td>Rua</td>
<td>Numero</td>
<td>Cidade</td>
<td>Estado</td>
<td>Usuário</td>
</tr>
@foreach($adresses as $adress)
<tr>
<td>{{$adress->id}}</td>
<td>
<a href="{{route('adress.details', ['adress'=>$adress])}}">
{{$adress->street}}
</a>
</td>
<td>{{$adress->number}}</td>
<td>{{$adress->city}}</td>
<td>{{$adress->state}}</td>
<td>
@foreach($users as $user)
@if($user->id == $adress->user)
{{$user->name}}
@endif
@endforeach
</td>
</tr>
@endforeach
</table>
This is the best way below? Could you explain me or tips?
<td>
@foreach($users as $user)
@if($user->id == $adress->user)
{{$user->name}}
@endif
@endforeach
</td>
Thank you!
I need help with this problem and not destructive criticism... HELP
– Rafael Blum