Good, I have a table called departaments (id, name) and a table called users (id, name, departament_id) that have a one-to-many relationship (The foreign key is in user), that is, a department has many users. When a user is created, the id is saved but when I want to show it in the list, only the id is shown instead of the name to which the id that is stored in the departaments table belongs.
<table class="table table-hover" style="background-color: white;">
<thead>
<tr>
<th width="20px">ID</th>
<th>CODIGO USUARIO</th>
<th>NOMBRE</th>
<th>APELLIDO</th>
<th>DEPARTAMENTO</th>
<th>CORREO</th>
<th>TELEFONO</th>
<th colspan="3"> </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id}}</td>
<td>{{ $user->user_code }}</td>
<td>{{ $user->name}}</td>
<td>{{ $user->lastName}}</td>
<td>{{ $user->departament_id}}</td>
<td>{{ $user->mail}}</td>
<td>{{ $user->phone}}</td>
<td>
<a href="{{ route('users.show',$user->id) }}" class="glyphicon glyphicon-zoom-in btn btn-primary btn-sm"> Ver</a>
</td>
<td>
<a href="{{ route('users.edit',$user->id) }}" class="glyphicon glyphicon-pencil btn btn-warning btn-sm"> Editar</a>
</td>
<td>
<form action="{{ route('users.destroy',$user->id) }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button class="glyphicon glyphicon-trash btn btn-danger btn-sm"> Eliminar</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
Edit1: The creation and editing of users is done perfectly, just that I need to show the value instead of the id of departaments, the name. This is the driver code for users
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Departament;
use App\Http\Requests\UserRequest;
class UserController extends Controller
{
public function index()
{
$users = User::orderBy('id', 'DESC')->paginate();
return view('users.index', compact('users'));
}
Public function create()
{
$departaments = departament::all();
return view('users.create', compact('departaments'));
}
public function store(UserRequest $request)
{
$user = new user;
$user->user_code = $request->user_code;
$user->name = $request->name;
$user->lastName = $request->lastName;
$user->mail = $request->mail;
$user->phone = $request->phone;
$user-> status = 1;
$user-> created_usr = $request->created_usr;
$user-> updated_usr = $request->updated_usr;
$user-> status_log = $request->status_log;
$user-> departament_id = $request->departament_id;
$user->save();
return redirect()->route('users.index')->with('info','El usuario '.$user->name.' fue creado exitosamente');
}
Public function edit($id)
{
$departaments = departament::all();
$user = User::find($id);
return view('users.edit', compact('user'), compact('departaments'));
}
public function update(UserRequest $request, $id)
{
$user = User::find($id);
$user->user_code = $request->user_code;
$user->name = $request->name;
$user->lastName = $request->lastName;
$user->mail = $request->mail;
$user->phone = $request->phone;
$user-> departament_id = $request->departament_id;
$user->save();
return redirect()->route('users.index')
->with('info','El usuario ha sido actualizado');
}
Public function show($id)
{
$user = User::find($id);
return view('users.show', compact('user'));
}
Public function destroy($id)
{
$user = User::find($id);
$user->delete();
return back()->with('info','El usuario fue eliminado');
}
}
Edit2: I add the departaments model that is the only one that has a relation (One department to many users)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Departament extends Model
{
protected $fillable = [
'name',
];
public function users()
{
return $this->hasMany('App\User');
}
}