Shows id and not the value of name that it has in table, laravel 5.5

0

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.

Attached the code of the list

<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">&nbsp;</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');
}
}
    
asked by Kinafune 02.01.2018 в 04:36
source

2 answers

0

The error is simple, you need the relationship belongsTo one to many inverse, to be able to obtain the department of a User. Just as your relations are, you can only obtain the users that belong to a department.

This relationship accepts as the second value the field name of the foreign key that should happen too.

In your Model User add the relationship

public function departamento()
{
    return $this->belongsTo('App\Departament','departament_id');
}

And from the View

<td>{{ $user->departamento->name }}</td>
    
answered by 02.01.2018 / 05:14
source
0

An alternative, using Query Builder, may be the following:

class UserController extends Controller
{
public function index()
{
    $users = User::join('departaments', 'departaments.id', '=', 'users.departament_id')
    ->select('users.*', 'departaments.name as departament_name')
    ->orderBy('users.id', 'DESC')
    ->paginate();

    return view('users.index', compact('users'));
}

And then, in the list, you simply replace with the following:

<td>{{ $user->departament_name}}</td>

Greetings

    
answered by 02.01.2018 в 18:57