使用一对一关系

时间:2018-03-02 14:31:52

标签: php laravel

我有两个表用户和技术人员一对一连接,技术人员继承用户。通过编辑表格编辑技术人员信息并保存后,表格用户和技术人员不会进行更新;也没有错误。

这是我的代码:

CONTROLER

public function edit($id)
{
$techniciens=technicien::find($id);
$user = $techniciens->user;
return view('technicien.edit',['techniciens'=>$techniciens])-
>with('user',$user);
}

public function update(Request $request, $id)
{
// do some request validation

$technicien=technicien::find($id);
$technicien->user->update($request->get('user'));
$user->update($request->all());

$technicien->update($request->all());
return redirect('/technicien');
}

查看

@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10">
            <h1>Modifier Technicien</h1>
        <form action="{{ route('technicien.update', $techniciens-
        >techniciens ) }}" method="update">
        {{csrf_field()}}
        {{ method_field('post') }}
            <div class="form-group">
            <label for="nom">Nom</label>
            <input id="nom" type="text" class="form-control" name="nom" 
            value="{{$user->nom}}" >
        </div>
        <div class="form-group">
            <label for="prenom">Prenom</label>
            <input id="prenom" type="text" class="form-control" 
            name="prenom" value="{{$user->prenom}}" >
        </div>
        <div class="form-group">
            <label for="prenom">Prenom</label>
            <input id="prenom" type="text" class="form-control" name="tel" 
            value="{{$user->tel}}" >
        </div>
        <div class="form-group">
            <label for="prenom">Prenom</label>
            <input id="prenom" type="text" class="form-control" name="mobil" 
            value="{{$user->mobil}}" >
        </div>
        <div class="form-group">
            <label for="prenom">Prenom</label>
            <input id="prenom" type="text" class="form-control" name="role" 
            value="{{$user->role}}" >
        </div>
        <div class="form-group">
            <label for="prenom">Email</label>
            <input id="prenom" type="text" class="form-control" name="email" 
            value="{{$user->email}}" >
        </div>
        <div class="form-group">
            <label for="prenom">Email</label>
            <input id="prenom" type="text" class="form-control" 
            name="password" value="{{$user->password}}" >
        </div>
   <div class="form-group">
            <label for="">moyenne Avis</label>
            <input type="text"name="moyenne_avis" class="form-control" 
            value ="{{$techniciens->moyenne_avis}}" >
        </div>
        <div class="form-group">
            <label for="">Etat Technicien</label>
            <input type="text"  name="actif" class="form-control" value ="
            {{$techniciens->actif}}" >
        </div>

        <div class="form-group">
            <input type="submit" value="enregistrer" class="form-control btn 
           btn-primary">
        </div>
            </div>
          </form>
    </div>
</div>
@endsection

路线

Route::get('/tache/{id}/edit', 'TacheController@edit');
Route::patch('/tache/{id}', 'TacheController@update')->name('tache.update');

MODEL1

 <?php

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

 class User extends Authenticatable
 {
protected $guarded = [];
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',
];
}

模型2

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class technicien extends Model
{
 protected $fillable = [
    'moyenne_avis', 'actif',
];
use SoftDeletes;
protected $guarded = [];
protected $dates = ['deleted_at'];
    public function zoneintervention()
{
    return $this->belongsToMany('App\zoneintervention','technicien_zone','technicien_id','zoneint
ervention_id');

}
    public function metier()
{
    return $this->belongsToMany('App\metier','technicien_metier','technicien_id','metier_id');

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

public function tarificationtache()
{
    return $this->belongsToMany('App\tarificationtache','technicien_tarificationtache','technicie
 n_id','tarificationtache_id');
}


}

0 个答案:

没有答案