IT2704
IT2704

Reputation: 163

how to display user's email instead of id

i have two tables user and technicien with a one to one connection in the index page i would like to display under user his email address and not his id.kindly help me with this. Here is my code (Model.user,Model.Technicien,index.blade.php) and a screenshot of the index.this might explain more what i want to do. Thank you.

Model.user

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
public function technicien()
{
    return $this->hasOne('App\technicien');

}

use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'email', 'password','nom','prenom','tel','mobil','role',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

Model.Technicien

public function user()
{
    return $this->belongsTo('App\User');
}

controller

public function index()
{


    $Listtechnicien=technicien::with(['user', 'zoneintervention'])->get();


    return view('technicien.index',['technicien'=>$Listtechnicien]);   

}

index.blade.php

@extends('Layouts.app')
@extends('Layouts.master')
@section('content')

<div class="container">
    <div class="row">
        <div class="col-md-10">


            <h1>Liste Technicien Technicien </h1>
            <div class="pull-right">
                <a href="{{url('technicien/create')}}">Nouveau 
    technicien </a>
            </div>
            <table class="table">

                <head>
                <tr>

                    <th>Utilisateur</th>
                    <th>actif</th>
                    <th>moyenne_avis</th>


                </tr>
                </head>
                <body>
                @foreach($technicien as $technicien)
                <tr>

                    <td>{{$technicien->user_id}}</td>
                    <td>{{$technicien->actif}}</td>
                    <td>{{$technicien->moyenne_avis}}</td>



                   <td>

                        <form action="{{url 
('technicien/'.$technicien->id)}}" method="post">
                            {{csrf_field()}}
                            {{method_field('DELETE')}}
                            <a href="{{url('technicien/'.$technicien->id.'/show')}}" class="btn btn-default" class="btn btn-primary">Details</a>
                            <a href="{{url('technicien/'.$technicien->id.'/edit')}}" class="btn btn-default">Editer</a>
                            <button type="submit" class="btn btn-
danger">Supprimer</button>
                        </form>

                    </td>
                </tr>
                    @endforeach


                    <script src="https://ajaxgoogleapiscom/ajax/libs/jquery/3.1.1/jquery.min.js">
                    </script>
                    <script type="text/javascript">
                        $(document).ready(function(){
                            $(document).on('change','.tachemetier',function(){
                                console.long("hmm its change");
                                });
                        });

                    </script>

                </body>
        </div>
    </div>
</div>
@endsection

enter image description here

Upvotes: 3

Views: 331

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

You've already defined the user() relationship and eager loading the date. So, just use the relationship:

{{ $technicien->user->email }}

Upvotes: 2

Prince Lionel N&#39;zi
Prince Lionel N&#39;zi

Reputation: 2588

In your blade file, change

 <td>{{$technicien->user_id}}</td>

To

 <td>{{$technicien->user->email}}</td>

Upvotes: 7

Related Questions