How to update Multiple row through post request in laravel - php

How to update multiple rows through post request in laravel when i submit the code it shows an error like Array to string conversion.
How can i solve this issue any body help thanks in advance?
post form here
<form class="form-horizontal" method="POST" action="{{ route('multiple') }}">
{{csrf_field()}}
#foreach($classes as $class)
<div class="">
<input type="hidden" class="form-control" name="id[]" value="{{$class->id}}">
<div class="col-md-12">
<input id="name" type="text" class="form-control" name="name[]" value="{{$class->name}}">
</div>
</div>
<div class="">
<div class="col-md-12">
<input id="class" type="text" class="form-control" name="class[]" value="{{$class->class}}">
</div>
</div>
#endforeach
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
submit
</button>
</div>
</div>
</form>
this is method
public function post_loop(Request $request)
{
foreach ($request as $data )
{
$values = array(
'name' => $request->name,
'class' => $request->class,
);
DB::table('multiple_data')->whereIn('id',$request->id)->update($values);
}
}

Your code is completely wrong. The request is a object and has many data about the request. But you are using it as an array to get your input.
Try this code.
$ids = $request->input('id');
$names = $request->input('name');
$classes = $request->input('class');
foreach($ids as $k => $id){
$values = array(
'name' => $names[$k],
'class' => $classes[$k],
);
DB::table('yourTable')->where('id','=',$id)->update($values);
}

Related

Update data in laravel, when there a data that is primary key

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

the put method is not supported

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">

Update not working in Resource Controller for Laravel 8

Previously, I was trying for a few hours to get my "destroy()" function working. Which would sound ridiculous to most of you but simply putting "method("DELETE")" in the form made it work, which I still don't why.
Now, I'm stuck with the update() function. Once I click update, the page just reloads and does nothing. It doesn't update the info in the database nor does it redirect me back to the index. Here's the code:
Route (web.php):
Route::resource('books', BookController::class);
Form (Edit.blade.php):
#section('content')
<div class="row">
<div class="col-lg-12">
<form action="{{ route('books.update', $book->id) }}" method="POST">
#csrf
#method('PUT')
<div class="mb-3 mt-3">
<label for="exampleFormControlInput1" class="form-label">Book Title</label>
<input type="text" name="title" class="form-control" value="{{ $book->title }}">
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Book Author</label>
<input type="text" name="author" class="form-control" value="{{ $book->author }}" placeholder="Author Name..">
</div>
<div class="btn-group" role="group">
<button type="submit" class="btn btn-block btn-danger">Update</button>
<button type="button" class="btn btn-success">Go Back</button>
</div>
</form>
</div>
</div>
#endsection
Controller (BookController):
public function update(Request $request, Book $book)
{
$validated = $request->validate([
'title' => 'required|max:255|unique:books',
'author' => 'required|max:255'
]);
Book::where('id',$book->id)->update($validated);
return redirect()->route('books.index')->with('message','Book Updated!');
}
What am I doing wrong here? Some help would be appreciated.
I think the problem is in your unique validation. When you don't change the title in the form and hit updated, the unique validation fails. It fails because the title already exists in the database. We have to skip unique validation for the resource that is being updated.
$validated = $request->validate([
'title' => 'required|max:255|unique:books,title,'.$book->id, // <-- change this
'author' => 'required|max:255'
]);
$book->update( $request->only(['title', 'author']) );

Error uploading multiples images with database saves

I have a problem when I add multiple images, they are saved in a file instead of the image being saved ...
Look
https://gyazo.com/de126b998c2967f421805e933b53b30b
My Function
public function createPage(Request $request)
{
$images = $request->input('images');
$pages = Pages::create([
'title' => $request->input('title'),
'meta_title' => $request->input('meta_title'),
'content' => $request->input('content')
]);
// Store each Images
foreach ($images as $image) {
$imagePath = Storage::disk('uploads')->put('test' .'/pages', $image);
Files::create([
'post_image_captation' => 'image',
'post_image_path' => '/uploads' . $imagePath,
'pages_id' => $pages->id
]);
}
return $pages;
}
my blade
<div class="modal-body">
<form method="post" action="{{ route('admin.pages') }}">
#csrf
<div class="form-group">
<label for="recipient-name" class="col-form-label">Title</label>
<input type="text" class="form-control" name="title" id="title">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Meta Title</label>
<input type="text" class="form-control" name="meta_title" id="meta_title">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Content</label>
<textarea class="ckeditor form-control" name="content" id="content"></textarea>
</div>
<div class="form-group">
<label for="images">Imagini</label>
<input type="file" class="form-control-file" name="images[]" id="images">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
Is there a solution to do what I created here in the easiest way possible?
Or is the logic good but I didn't put it into practice?
I think you modify a bit .Instead of Storage put you can use storeAs
if(isset($images)&&count((array)$images)){
foreach ($images as $image) {
$fileName = time().Str::random(8).'_'. $image->getClientOriginalName();
$image->storeAs('test/pages/', $fileName, 'uploads');
Files::create([
'post_image_captation' => 'image',
post_image_path' => '/uploads' . $fileName,
'pages_id' => $pages->id
]);
}
}

laravel foreach input radiobutton displayed incorrectly

I'm trying to display the options for the questions. i have the problem that when im doing it this way:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
i can check all radio buttons at the same time but if i put there a name for the radiobutton i can check only one of the options for the whole questions.. i think there's something strange with the foreach loop..
Info: the table "questions" has the following rows:
id, category_id, question_text, correct_answer, option_text (this is a json-field that is casted to an array)
Code from Controller:
public function store(Request $request, Category $category){
if($request->categoryTest == $category->name){
$questions = $category->question()->inRandomOrder()->get();
return view('user.test', compact('questions', 'category'));
}
}
do you have an idea how to fix this? thank you!
Try this:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
#php
$q = 1+$key
#endphp
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input name="radio-{{$q}}" type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
This seems to be a normal behavior of the radio buttons, only one is market at time. Have you tried to use checkbox instead?
Also, if you want to send it to backend, you'll need to set a name attribute. And here is the magic: instead of using a singular name, put it as array in order to get an array on your controller.
<input type="checkbox" name="question[]" class="btn btn-primary">

Categories