I have three different tables and different models. The tables are State Office, City Office, Hometown Office. I want to show the data of the office that the user will select between these three offices. There is a column in the user table called office_id. I'm brand new to Laravel so I don't know-how. Would you please tell me how to do this?
I have already add some code to my User Model
public function stateoffice()
{
return $this->hasOne(State::class, 'id', 'office_id');
}
public function cityoffice()
{
return $this->hasOne(CityOffice::class, 'id', 'office_id');
}
public function hometownoffice()
{
return $this->hasOne(HometownOffice::class, 'id', 'office_id');
}
This is my controller code
public function AllUser(){
$data['user'] = User::all();
$data['stateoffice'] = StateOffice::all();
$data['cityoffice'] = CityOffice::all();
$data['hometownoffice'] = HometownOffice::all();
return view('admin.all-user', $data);
}
But I'm stuck in View how can I show it.
<thead>
<th>Office Name</th>
</thead>
<tbody>
@foreach($user as $data)
<tr>
<td>@if($data->office_id == NULL)
<span>no data found</span>
@else
{{$data->office->office_name}}
@endif
</td>
</tr>