/ / Update auf "Benutzer" und "Technik" mit Eins-zu-Eins-Verbindung - Laravel, Laravel-5, Laravel-5.2, Laravel-5.1

Update auf "Benutzer" und "Technik" mit Eins-zu-Eins-Verbindung - Laravel, Laravel-5, Laravel-5.2, Laravel-5.1

Ich habe zwei Tische Benutzer und Techniker mit einem zuEine Verbindung erbt der Techniker vom Benutzer. Nach dem Bearbeiten der Technikerinformationen über das Bearbeitungsformular und dem Speichern erfolgt keine Aktualisierung des Tabellenbenutzers und des Technikers. und auch keine fehler.

Hier ist mein Code:

Controller

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

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

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

return redirect("/technicien");
}

Aussicht

 @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", $technicien->technicien  ) }}" method="update">
{{csrf_field()}}
{{ method_field("PATCH") }}


<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-  control" name="user[email]" value="{{$user->email}}" >
</div>

<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text"  name="moyenne_avis" class="form-control" value ="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text"  name="actif" class="form-control" value ="{{$technicien->actif}}" >
</div>

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




</form>
</div>
</div>
@endsection

Route.php

Route::get("/technicien/{id}/edit", "TechnicienController@edit");
Route::patch("/technicien/{id}", "TechnicienController@update")->name("technicien.update");

Modell1

<?php

namespace App;

use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;
class technicien extends Model
{
protected $fillable = [
"moyenne_avis", "actif",
];
use SoftDeletes;
protected $guarded = [];
protected $dates = ["deleted_at"];
public function zoneintervention()
{
return $this->belongsToMany("Appzoneintervention","technicien_zone","technicien_id","zoneintervention_id");

}
public function metier()
{
return $this->belongsToMany("Appmetier","technicien_metier","technicien_id","metier_id");

}
public function user()
{
return $this->belongsTo("AppUser");
}

public function tarificationtache()
{
return $this->belongsToMany("Apptarificationtache","technicien_tarificationtache","technicien_id","tarificationtache_id");
}


}

Modell2

<?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable
{
public function technicien()
{
return $this->hasOne("Apptechnicien");

}

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",
];
}

Bildbeschreibung hier eingeben

Bildbeschreibung hier eingeben

Antworten:

1 für die Antwort № 1

Controller

public function edit($id)
{
// better to use findOrFail (it will throw an exception about missing
objects)
$technicien = technicien::findOrFail($id);
return view("technicien.edit", compact("technicien"));
}

public function update(Request $request, $id)
{
$technicien=technicien::findOrFail($id);
$technicien->user->update($request->get("user"));
$technicien->update($request->get("technicien"));
return redirect("/technicien");
}

und die Aussicht

@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", $technicien ) }}"
method="post">
{{csrf_field()}}
{{ method_field("patch") }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control"
name="user[nom]" value="{{$technicien->user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[prenom]" value="{{$technicien->user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[tel]" value="{{$technicien->user->tel}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[mobil]" value="{{$technicien->user->mobil}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[role]" value="{{$technicien->user->role}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control"
name="user[email]" value="{{$technicien->user->email}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control"
name="user[password]" value="{{$technicien->user->password}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text"  name="technicien[moyenne_avis]"
class="form-control" value="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text"  name="technicien[actif]"
class="form-control" value="{{$technicien->actif}}" >
</div>

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

0 für die Antwort № 2

Versuchen Sie es mit einer Eins-zu-Eins-Beziehung. Stellen Sie eine Beziehung für die Benutzertabelle und die Technikertabelle her und versuchen Sie dann, eine Aktualisierung durchzuführen.


0 für die Antwort № 3

Versuchen Sie dies in der Steuerung

$user = User::with("technicien")->find($id);
$data["id"] = $id;

$data = $this->validate($request, [
"moyenne_avis" => "required",
]);

$user->technicien()->whereUserId($data["id"])->update(["moyenne_avis" => $data["moyenne_avis"]
]);

return redirect("/technicien")->with("Success", "Records updated");

Ändern Sie auch die Formularmethode wie unten in ur view.blade

<form action="{{ action("TechnicienController@update", $user->id) }}" method="post">

auch anstelle von {{ method_field("PATCH") }} benutze das <input name="_method" type="hidden" value="PATCH">

Hinweis: Der Tabellenname sollte für den Controller- und Modellnamen Plural sein.
Beispiel: Tabellenname: Benutzer Controller-Name: UserController Modellname: Benutzer
Stellen Sie sicher, dass dies auch bei uns der Fall ist.