I want some help with my code.
I try to update user data but it's not update anything.
User Name, Email, Posisson, Image.
Any help please.
My Route :
I used URL because route didn't work.
Route::get('editusers/{id}','UsersController#update');
My Controller:
public function edit($id)
{
$editusers=User::findOrFail($id);
return view('admin.users.EditUser', compact('editusers'));
}
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'posission' => 'required',
]);
$useredit = User::find($id);
$useredit->name = $request->input('name');
$useredit->email = $request->input('email');
$useredit->posission = $request->input('posission');
if($request->hasFile('file'))
{
$file = $request->file('file');
$filename = time().'.'.$file->getClientOriginalExtension();
Image::make($file)->resize(150, 150)->save(public_path('/admin/images/'.$filename));
$useredit->UserImg = $filename;
}
$useredit->save();
return redirect()->back();
}
HTML :
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
<label>Edit Your Profile :</label>
<div class="form-group">
<label>Name :</label>
<input class="form-control" value="{{$editusers->name}}" name="Name">
</div>
<div class="form-group">
<label>Email :</label>
<input class="form-control" value="{{$editusers->email}}" name="email">
</div>
<div class="form-group">
<label>Posisson :</label>
<input class="form-control" value="{{$editusers->posission}}" name="posission">
</div>
<div class="form-group">
<label>Image :</label>
<img src="{{ asset('admin') }}/images/{{$editusers->UserImg}}" alt="avatar" class="img-circle" style="max-height: 100px;">
<input type="file" id="file" name="file"/>
</div>
<input class="btn btn-success btn-mini deleteRecord" type="submit" name="submit" value="Update">
What I expect is that it updates my database.
As your form has
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
so your route must have put(),
so it should be Route::put('editusers/{id}','UsersController#update');
also you can use #method('PUT') instead of <input type="hidden" name="_method" value="PUT"> and #csrf instead of {!! csrf_field() !!}
either change $useredit->name = $request->input('name'); to $useredit->name = $request->input('Name'); or in form
<input class="form-control" value="{{$editusers->name}}" name="Name"> to
<input class="form-control" value="{{$editusers->name}}" name="name">
Your route is wrong
Route::get('editusers/{id}','UsersController#update');
it supposed to be PUT
Route::put('editusers/{id}','UsersController#update');
The problem because you put wrong method at your route. Change it
// From
Route::get('editusers/{id}', 'UsersController#update')
// To
Route::put('editusers/{id}', 'UsersController#update')
Anyways, you should change your route to be standard. It should be:
//To show data you should use:
Route::get('users/edit/{id}', 'UsersController#show');
//To update user data.
Route::put('users/edit', 'UsersController#update');
i think you should use resources route to solve this:
Route::resource('editusers','UserController');
but first you need to run this command
php artisan make:controller UserController --resource
Related
I need some advice. I make a view for editing a database that contains textarea form. Unfortunately, when I hit the save button it doesn't give any change to the database. What should I do?
Here's the form in infoupdate.blade.view:
<form action="{{ route('updateinfo') }}" method="POST">
#method('PUT')
#csrf
<div class="intro-y box p-5">
<div>
<div class="mt-3"> <label for="by" class="form-label">Oleh</label> <input name="by" id="by" type="text" class="form-control" value="{{ auth()->user()->username }}" readonly></div>
</div>
<div>
<div class="mt-3"> <label for="selectedTime" class="form-label">Waktu Pengumuman</label> <input name="selectedTime" id="selectedTime" type="text" class="form-control" value="{{ $infos->selectedTime }}" readonly></div>
</div>
<div class="mt-3">
<label class="pb-8" for="contentInfo">Isi Pengumuman</label>
<textarea name="contentInfo" id="contentInfo" class="w-full border-2" rows="10" cols="100">{{ $infos->contentInfo }}</textarea>
</div>
<div class="text-right mt-5">
<button type="submit" class="btn btn-primary w-24">Simpan</button>
</div>
</div>
</form>
Also the update function in DBIController.php:
public function update(Request $request){
$request->validate([
'contentInfo' => 'required|min:16'
]);
DB::table('infos')->where('id', $request->id)->update([
'contentInfo' => $request->contentInfo
]);
return redirect()->route('DBI')->with('message','Data Pengumuman Berhasil di Update');
}
and the route for displaying the form and the route for updating database in web.php :
Route::get('/infoeditor/{id}',[DBIController::class, 'edit'])->middleware('admin')->name('infoeditor');
Route::put('/updateinfo',[DBIController::class, 'update'])->middleware('admin')->name('updateinfo');
on your update route, you need to add /{id}
Route::put('/updateinfo/{id}',[DBIController::class, 'update'])->middleware('admin')->name('updateinfo');
Then, in your controller method, you will add $id as parameter
public function update(Request $request, int $id)
{
$request->validate([
'contentInfo' => 'required|min:16'
]);
DB::table('infos')->where('id', $id)->update([
'contentInfo' => $request->contentInfo
]);
return redirect()->route('DBI')->with('message','Data Pengumuman Berhasil di Update');
}
And in your view, you update the action url with $id
<form action="{{ route('updateinfo', ['id' => $infos->id]) }}" method="POST">
I m using laravel 8 controller for saving text input type using "Store function".My input for 'name' are not stored in database when i submited it.
Here is my blade
<form class="text-center p-4" action="{{ route('store') }}" method = "POST">
#csrf
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">names</label>
<input type="text" class="form-control" id="name" placeholder="name">
</div>
<button class="btn btn-primary" type="submit">Button</button>
</form>
Here is my ProductController.php
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
Product::create($request->all());
$products -> save();
return redirect('/saving-list') ->with('success','Umrah record has been updated');
}
your parameter is not complete, 'detail' => 'required' but it's not found
you need new value input <input type="text" class="form-control" id="detail" placeholder="detail" name="detail">
and you have to add attribute name in every input
for complete code like this bellow
<form class="text-center p-4" action="{{ route('store') }}" method = "POST">
#csrf
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">names</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
<input type="text" class="form-control" id="detail" name="detail" placeholder="detail">
</div>
<button class="btn btn-primary" type="submit">Button</button>
</form>
and for save or store method https://laravel.com/docs/8.x/eloquent#inserts
$product= new Product;
$product->name = $request->name;
$product->detail= $request->detail;
$product->save();
or like this
$product= Product::create([
'name' => $request->name,
'detail' => $request->detail,
]);
I want to do following system without any laravel library, but when I submit form not saving anything. Where is the problem I don't know.
My route is:
Route::post('/follow' , [HomeController::class, 'follow'])->name('follow');
My blade is:
<form class="form-horizontal" action="{{route('follow')}}" method="POST"></form>
#csrf
<div class="form-group">
<input class="form-control" type="hidden" name="follower_id">
<input type="submit" class="site-btn" name="following_id" value="Follow" >
</div>
</form>
My controller is:
public function follow(Request $request){
$request->validate([
'follower_id'=>['required'],
'following_id'=>['required'],
]);
$follower_id = $request->follower_id;
$following_id = $request->following_id;
$save = Follow::create([
'following_id' => Auth::user()->id,
'follower_id' => $follower_id,
]);
if($save){
return back();
}else{
return back();
}
}
And my User model contains the following relationship
public function follows(){
return $this->hasMany('App\Models\Follow');
}
remove </form> in first line
<form class="form-horizontal" action="{{route('follow')}}" method="POST"></form>
#csrf
<div class="form-group">
<input class="form-control" type="hidden" name="follower_id">
<input type="submit" class="site-btn" name="following_id" value="Follow" >
</div>
</form>
it should be like this
<form class="form-horizontal" action="{{route('follow')}}" method="POST">
#csrf
<div class="form-group">
<input class="form-control" type="hidden" name="follower_id">
<input type="submit" class="site-btn" name="following_id" value="Follow" >
</div>
</form>
Controllers/HomeController.php
public function edit(Task $task)
{
return view('edit', compact('task'));
}
public function update(Request $request, Task $task)
{
$request->validate(['title' => 'required|min:3', 'description' => 'required', ]);
$task->title = $request->title;
$task->description = $request->description;
$task->save();
$request->session()
->flash('message', 'Successfully modified the task!');
return redirect('viewalltask');
}
routes/web.php
Route::post('/{task}/', 'HomeController#update')->name('update');
views/edit.blade.php
<form action="{{url('', [$task->id])}}" method="POST">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field() }}
<div class="row">
<div class="col-md-3" >
<label for="title" >title</label>
<input id="title" type="text" class="form-control" name="title" value="{{$task->title}}" required autofocus>
</div>
<div class="col-md-3">
<label for="description" >description</label>
<input id="description" type="text" class="form-control" name="description" value="{{$task->description}}" required>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary" >
Edit
</button>
</div>
</div>
</form>
Error :
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
In your routes file the you have declared the wrong method . it should be like this.
Route::put('/{task}', 'HomeController#update')->name('update');
You are using wrong method type in routes as compare to method type in form,
Route::post('/{task}/', 'HomeController#update')->name('update');
Should work.
My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.