laravel edit and update doesn't work - laravel

I have a CRUD with simple date. But I can't get the update method to work.
This is my controller
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, Customer
$customer,$id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
and that is my view
<form method="POST" action="{{route('customer.update',$customer->id ) }}">
{{--{{dd($customer)}}--}}
{{method_field('PUT')}}
{{ csrf_field() }}
<div class="form-group">
<label for="firstName">Voornaam</label>
<input type="text" class="form-control" name="firstName" value="{{$customer->firstName}}">
</div>
<div class="form-group">
<label for="lastName">Achternaam</label>
<input type="text" class="form-control" name="lastName" value="{{$customer->lastName}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" value="{{$customer->email}}">
</div>
<div class="form-group">
<label for="phone">Telefoonnummer</label>
<input type="text" class="form-control" name="phone" value="{{$customer->phone}}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
I can go to the edit page. But, after changing the data nothing change it look like that I miss save somewhere but I don't know, now I get that that error too few arguments to function New Controller ::update () 2 passed and exactly 3 expected.
any help will be appreciated.

Change:
public function update(CustomerRequest $request, Customer
$customer,$id)
To:
public function update(CustomerRequest $request, $id)
Or:
public function update(CustomerRequest $request, Customer
$customer)
With the last one you can remove Customer::find($id) and just use $customer
EDIT
If you look at the update route: route('customer.update',$customer->id ) you see it only takes 1 argument. The controller expects 2 because one is the request and the other is the ID.

Related

request has file is not working - laravel

public function store(Request $request) {
if($request->hasFile('image')) {
return "this is if";
} else {
return 'this is else';
}
}
This is my function. The print is "this is else". Why it's not going inside if?
public function store(Request $request) {
dd($request);
}
It is giving us:
public function store(Request $request) {
dd($request->all());
}
It is giving us:
So, I'm taking an 'image', but the hasFile function does not see it. How to fix it?
Extra: This is my form section:
<form method="post" action="{{ route('articles.store') }} " enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="">Title</label>
<input type="text" name="title" class="form-control" required>
</div>
<div class="form-group">
<label for="">Category</label>
<select name="category" required>
#foreach($categories as $category)
<option value="{{$category->id}}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Article Image</label>
<input type="file" name="image" class="form-control" required>
</div>
<div class="form-group">
<label for="">Article Content</label>
<textarea name="contentArticle" id="summernote" rows="12"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-block">Create an Article</button>
</div>
</form>
Have you try to do this ?
public function store(Request $request)
{
dd($request->all());
{
What result do you get ?
You have your enctype in your frontend code, there is not any problem just run the below commands:
composer dump-autoload
php artisan optimize:clear
If again that does not work then use the below code for checking if you have an image or not:
if ($request->file('image')!=null){...}
In place of:
if($request->hasFile('image')){...}

Laravel : no errors and no update on database happens after saving the edit

I have two tables, user and technicien, with a one to one relation. After editing technicien information through edit form and saving, no update happens on database and no errors as well.
Here is my code:
controllers
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');
}
View
#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');
You just need to pass parameters to update function.
Read docs
public function update(Request $request, $id)
{
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update([
'nom' => $request->nom,
'premon' => $request->premon,
'email' => $request->email
]);
return redirect('/technicien');
}
Also from the docs
You should define which model attributes you want
to make mass assignable. You may do this using the $fillable property
on the model.

Update on "user" and "technicien" with one to one connection

I have two tables user and technician with one to one connection, technician inherits from user. After editing technician information through edit form and saving no update happens on tables user and technician. and no errors as well.
Here is my code:
Controllers
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');
}
View
#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');
Model1
<?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','zoneintervention_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','technicien_id','tarificationtache_id');
}
}
Model2
<?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',
];
}
controler
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');
}
and the view
#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
Try to do with One to One relation.
Make relation for user table and technician table, then try to do update.
Try this in controller
$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');
Also change form method as below in ur view.blade
<form action="{{ action('TechnicienController#update', $user->id) }}" method="post">
also, instead of {{ method_field('PATCH') }} use this <input name="_method" type="hidden" value="PATCH">
Note: Table name should be plural for controller and model name.
eg: Table name: users
Controller name: UserController
Model name: User
Make sure this same in urs too.

Update Logged in user data in Laravel5

I want to update the profile of logged in users in laravel5. I have the name of the logged in user through the session but then when I want to put the new input data to the old user data, it didn't work. I think I am having a problem in getting input user data into the controller function.
Controller:
public function updateProfile(Request $req) {
$name = Session::get('admin-name');
$user = DB::table('admin')->where('name',$name)->first();
return dd($req->input('admin-name'));
if($req->input('admin-name')!= null) {
$user->name = $req->input('admin-name');
}
if($req->input('admin-email')!= null) {
$user->email = $req->input('admin-email');
}
if($req->input('admin-address')!= null) {
$user->email = $req->input('admin-address');
}
if($req->input('admin-mobile')!= null) {
$user->mobile = $req->input('admin-mobile');
}
if($req->input('admin-dob')!= null) {
$user->dob = $req->input('admin-dob');
}
$user->save();
return view('admin-profile')->with('update-response','Profile Updated successfully');}
View:
<form method="post" action="{{route('admin-edit-profile')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="admin-name">Name:</label>
<input type="text" class="form-control" name="admin-name" id="admin-name" placeholder="Enter your name"/>
</div>
<div class="form-group">
<label for="admin-email">Email:</label>
<input type="text" class="form-control" name="admin-email" id="admin-email" placeholder="Enter your email"/>
</div>
<div class="form-group">
<label for="admin-address">Address:</label>
<input type="text" class="form-control" name="admin-address" id="admin-address" placeholder="Enter your address"/>
</div>
<div class="form-group">
<label for="admin-mobile">Mobile:</label>
<input type="text" class="form-control" name="admin-mobile" id="admin-mobile" placeholder="Enter your mobile number"/>
</div>
<div class="form-group">
<label for="admin-dob">Date of Birth:</label>
<input type="date" class="form-control" name="admin-dob" id="admin-dob" placeholder="Enter your Date of Birth"/>
</div>
<div class="form-group">
<label for="admin-pic">Your Profile pic:</label>
<input type="file" class="form-control" name="admin-pic" id="admin-pic" placeholder="Your Profile pic"/>
</div>
<input type="submit" value="Save" id="submit" class="btn btn-info"/> <input type="reset" value="Reset" id="reset" class="btn btn-info"/>
</form>
Routing is working fine. But I think the problem is with the controller function.I am getting an error:
Call to undefined method stdClass::save()
Getting the error: Call to undefined method stdClass::save()
save() in Eloquent method, so use Eloquent:
$user = Admin::where('name', $name)->first();
in your case you should use eloquent by replace
$user = DB::table('admin')->where('name',$name)->first();
with
$user = Admin::where('name', $name)->first();
if(!$user){ return "can't find this admin the name is null or not saved in DB"; }
Certainly you should import your Admin model
note: be sure if admin-name is Session key

How to autofill Edit form with old Information from two different tables?

I have this form "Edit tache". Editing one "tache" affects two tables user and technicien, when i choose to edit tache the edit form is autofilled with information from the technicien table but the fields related to the table user are not autofilled. How could i autofill the form with information from the two tables? Thank you.
controller
public function edit($id)
{
$technicien=technicien::find($id);
$user = user::orderBy('id', 'asc')->get();
return view('technicien.edit',['moyenne_avis'=>$technicien],['actif'=>$technicien],['user_id'=>$technicien])->with('users', $user );
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
return redirect('technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Tache</h1>
<form action="{{ route('technicien.update', $actif->id , $moyenne_avis->id ) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">Nom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">Prenom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-control"value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name ="actif" class="form-control"value ="{{$actif->actif}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
route
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::put('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
$user = user::find($id);
if they are retrieved using one id on the controller, else please tell the relation between them.
and in your view, i don't see a reason to check old since its not updated yet! use
{{$user->nom)}}

Resources