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,
]);
Related
i want to edit data, and the id is primary key.
but when i submit, the database does not change.
here the code
controller :
public function edit($id)
{
$karyawan = Karyawan :: find ($id);
$jabatan = Jabatan::all();
return view ('/karyawan.editData', compact(('karyawan'),('jabatan')));
}
public function update(Request $request, $id)
{
$request->validate([
'id' => 'required',
'nama'=> 'required',
'umur'=>'required',
'alamat'=> 'required',
'nomor'=> 'required'
]);
$karyawan =Karyawan::find($id);
$karyawan->jabatan_id = $request->jabatan;
$karyawan->nama = $request->nama;
// $karyawan->id = $request->id;
$karyawan->umur = $request->umur;
$karyawan->alamat = $request->alamat;
$karyawan->nomor = $request->nomor;
$karyawan->save();
return redirect ('/karyawan');
}
and the editData :
#extends ('layout')
#section ('content')
<h1>Edit Data Karyawan </h1>
<form action="/karyawan/{{ $karyawan->Id }}" method="post">
{{-- <form action = "/karyawan" method="post"> --}}
#csrf
<div class="form-group">
<label for = "title">ID Jabatan</label>
<select name="jabatan" class="form-control">
#foreach ($jabatan as $baris)
<option value = "{{ $baris->id}}">{{ $baris->nm_jabatan }}</option>
#endforeach
</select>
<div class = "form-group">
<label for="title"> nama</label>
<input type="text" class="form-control" name="nama">
</div>
<div class = "a">
<label for="title"> ID </label>
<input type="number" class="form-control" name="id" value = "{{ old('id') ? old('id'): $karyawan->id }}">
</div>
<div class = "form-group">
<label for="title"> umur</label>
<input type="text" class="form-control" name="umur">
</div>
<div class = "form-group">
<label for="title"> alamat</label>
<input type="text" class="form-control" name="alamat">
</div>
<div class = "form-group">
<label for="title"> nomor</label>
<input type="text" class="form-control" name="nomor">
</div>
<a href="/karyawan">
<button type = "button" class="btn btn-warning">kembali</button>
</a>
<button type ="submit" class = "btn btn-primary">Simpan</button>
</form>
#endsection
how i can update the data? from these code
i expect to get some explanation, and show me how to fix my code. so i can submit the update data on my database.
Try this in your controller
public function update(Request $request, $id)
{
$validated_data = $request->validate([
'name' => 'required|max:255', (add all the form here)
]);
YourModel::where('id', $id)->update($validated_data);
return redirect('admin/problem')->with('success', 'Data berhasil diubah');
}
Tips* dump all the request with dd($request->all) so you can see all of your input form data
when i try to update my the section name i am having this error: The PUT method is not supported for this route. Supported methods: GET, HEAD.
there is the form:
<form action="sections.update" method="POST" autocomplete="off">
#csrf
#method('PUT')
<div class="form-group">
<input type="hidden" name="id" id="id" value="">
<label for="recipient-name" class="col-form-label">section name</label>
<input class="form-control" name="section_name" id="section_name" type="text">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">description</label>
<textarea class="form-control" id="description" name="description"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">confirm</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
</div>
</form>
and that is the update controller
public function update(Request $request)
{
$id = $request->id;
$this->validate($request, [
'section_name' => 'required|max:255|unique:sections,section_name,' . $id,
'description' => 'required',
], [
'section_name.required' => 'section name is required',
'section_name.unique' => 'section_name should be unique',
'description.required' => 'description is required',
]);
$sections = sections::find($id);
$sections->update([
'section_name' => $request->section_name,
'description' => $request->description,
]);
session()->flash('edit', 'the section is edited successfully');
return redirect('/sections');
}
you have to pass the id/parameter in your form action when using put method but u re fetching id from form , so you can pass some dummy data on it. Change the form to
<form action="action="{{ route('sections.update', ['section' => '1']) }}" method="POST" autocomplete="off">
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 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
I have tried everything and I can't figure out where comes my mistake.
the update() method doesn't update anaything, i only get back "No message" error...
Here's Routes in web.php:
Route::get('/user/edit/{id}', ['as' => 'users.edit', 'uses' => 'UserAdController#edit']);
Route::post('/user/update/{id}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
the view users/edit.blade.php :
<div class="container">
<br>
<h3>Edit your ad</h3>
<br>
<form method="post" action="{{route('users.update', $ad->id)}}">
<input name="_method" type="hidden" value="PATCH">
{{ method_field('post') }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" value="{{$ad->title}}">
</div>
<div class="form-group">
<label for="title">Price</label>
<input type="text" name="price" class="form-control" id="title" value="{{$ad->price}}">
</div>
<div class="form-group">
<label for="content">Your content</label>
<textarea name="content" class="form-control" id="content" rows="3">{{$ad->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-info">
</div>
</form>
</div>
#endsection
Update method from UserAdController:
public function update($id, Request $request){
$request->validate([
'title'=>'required',
'price'=> 'required|integer',
'content' => 'required'
]);
$data = \App\Ad::find($id);
$data->title = $request->get('title');
$data->price = $request->get('price');
$data->content = $request->get('content');
$data->save();
return redirect()->back()->with('success', 'Data updated');
}
I'm not a laravel dev. I just stumbled on the documentation. You should also add the csrf field to your blade
In edit.blade.php, add this after the opening <form> tag
{{csrf_field()}}
Also the parameters in your update method aren't well arranged
It should be
public function update(Request $request, $id) {
}
The second parameter($id), comes from what you've defined as your routes in web.php file
Route::post('/user/update/{id}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
Where {id} would be replaced by the original id
Try this instead
public function update(Request $request){
//your code here
}
Request->only() returns an array with one element, While validator is the most common way to handle validation for the incoming request.
use Validator;
public function update(Request $request, $id){
$v = validator($request->only('title', 'price', 'content'), [
'title' => 'required|string|max:255',
'price' => 'required|integer',
'content' => 'required',
]);
$data = request()->only('title','price','content');
$userData = ([
'title' => $data['title'],
'price' => $data['price'],
'content' => $data['content'],
]);
$data = \App\Ad::find($id);
$data->update($userData);
return response()->json($data);
}
Thank you all !!
Seems like I wasn't doing it right.
I needed to add {{csrf_field()}} in edit form and use $request->only()
I think it would be better if you use put method like this:
Route::put('ad/{ad}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
update your form to be like this:
<div class="container">
<br>
<h3>Edit your ad</h3>
<br>
<form method="post" action="{{route('users.update', ['ad' => $ad->id])}}">
<input name="_method" type="hidden" value="PATCH">
{{ method_field('put') }}
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" value="{{$ad->title}}">
</div>
<div class="form-group">
<label for="title">Price</label>
<input type="text" name="price" class="form-control" id="title" value="{{$ad->price}}">
</div>
<div class="form-group">
<label for="content">Your content</label>
<textarea name="content" class="form-control" id="content" rows="3">{{$ad->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-info">
</div>
</form>
and now your update function :
public function update(\App\Ad $ad, Request $request){
$request->validate([
'title'=>'required',
'price'=> 'required|integer',
'content' => 'required'
]);
//$data = \App\Ad::find($id);
$ad->update([
"title" => $request->title,
"price" => $request->price,
"content" => $request->content,
]);
return redirect()->back()->with('success', 'Data updated');
}
when you get use to put, delete and patch methods you can read about Route::resource and your code will be easier.