如何显示用户的电子邮件而不是ID

时间:2018-02-23 15:26:24

标签: laravel laravel-5 laravel-5.2 laravel-5.1 laravel-5.3

我有两个表用户和技术人员在索引页面中有一对一连接我想在用户下显示他的电子邮件地址而不是他的id.kindly帮我这个。这是我的代码(Model.user,Model.Technicien,index.blade.php) 以及索引的截图。这可能更多地解释了我想要做的事情。谢谢。

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');
}

控制器

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

2 个答案:

答案 0 :(得分:7)

在您的刀片文件中,更改

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

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

答案 1 :(得分:2)

您已经定义了user()关系并急切加载日期。所以,只需使用关系:

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