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

how to display user's email instead of id

我有两个表 user 和 technicien,在索引页面中有一对一的连接,我想在用户下显示他的电子邮件地址,而不是他的 id.kindly 帮我解决这个问题。这是我的代码 (Model.user,Model.Technicien,index.blade.php) index.this 的屏幕截图可能会更多地解释我想做什么。谢谢。

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

在您的 blade 文件中,更改

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

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

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

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