Checkbox value in controller in laravel - laravel

I am trying to get all the payments that is been selected through the checkboxes and then update the value in the database by '1' which means its paid. But when I try to get the values of checkbox array I only get 1 data in the array and not all selected. Any help?
View
#foreach($sellerID as $p)
<form action="/paid/{{$p->id}}" method="post" enctype="multipart/form-data">
#csrf
<td><input type="checkbox" name="checked[]" value="{{ $p->id }}"></td>
<td> {{$p->order_number}} </td>
<td>
<input id="commission" type="text" class="form-control" name="commission"
value="{{ $p->commission_value }}" readonly autocomplete="notes" style="width: 15%;" autofocus>
</td>
<td> {{$p->product_name}} </td>
<td>
#if($p->sellet_commission_paid == '0')
<button type="submit" class="btn btn-default" style="background-color:red">
<b><em>UNPAID</b></em>
</button>
#else
<b><em>PAID</b></em>
#endif
<td>
</form>
#endforeach
Controller
$checked = $request->input('checked');
// $checkboxes = $request->checked;
dd($checked);
die();
// dd(checkboxes);

you can put almost anything you like inside a form, but not inside a table. table has a more restricted structure which you must follow.
instead of wrapping table rows with forms, you can wrap the whole table with 1 form and submit all of it and just extract the details you need,
You need to put all input element inside form :
<form action="/route">
<input ...../>
<div>
<input ...../>
</div>
</form>
A form-associated element is, by default, associated with its ancestor form element, but may have a form attribute specified to override this.
If a form-associated element has a form attribute specified, then that attribute's value must be the ID of a form element in the element's owner Document.

Try using a foreach loop.
foreach($request->input('checked') as $check){
[sample variable] = $check;
}
The array will now be stored as $check and you should be able to get each value from the input

Related

Displaying individual errors Inertia Vue - Laravel App

So I have a dynamic table, where you can add as many services as you want, you can select how many of them and if you want to have a discount for each service
<tr v-if="proceduresList.length" v-for="(procedure,index) in proceduresList" :key="procedure.id">
<td>{{ procedure.name }}</td>
<td>
<input v-model="procedure.amount" type="number" step="any" class="form-control">
</td>
<td>
<input v-model="procedure.discount" type="number" step="any" class="form-control">
</td>
<td class="text-center">
<button class="btn btn-danger btn-sm" #click="removeProcedure(procedure)"><i class="fa fa-trash"></i></button>
</td>
</tr>
I have a validation for the procedures array in my laravel backend whick, In case of errors, returns something like this
errors:Object
description:"The description field is required."
procedures.0.amount:"The procedures.0.amount field is required."
procedures.1.discount:"The procedures.1.discount field is required."
procedures.2.amount:"The procedures.2.amount field is required."
procedures.2.discount:"The procedures.2.discount field is required."
start_date:"The start date field is required."
What I want to do is to display the individual errors for each row, I haven't figured out how.
I tried like this
<input v-model="procedure.amount" type="number" step="any" class="form-control">
<span v-if="attentionForm.errors.procedures[index].amount">{{ attentionForm.errors.procedures[index].amount }}</span>
But when I add a service / procedure to the table, the page goes blank and in the debugger it says:
TypeError
Cannot read properties of undefined (reading '0')
You can use square brackets to access the error property:
<span v-if="attentionForm.errors[`procedures.${index}.amount`]">
{{ attentionForm.errors[`procedures.${index}.amount`] }}
</span>
Example here: https://youtu.be/LILSriakRZA?t=547

How to get checkbox value in a Laravel controller?

I have a checkbox in my form and I want to save its value into the database. When I check it, its value will be 1 and when I uncheck it then it's value will be 0. However, in the controller, I am getting NULL all the time. But why? Is it possible to solve without jQuery?
Blade/View
<form action="{{ route('admin.categories.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group>
<label>
<input type="checkbox" name="category_is_menu"
value="{{ old('category_is_menu', isset($category) ? 'checked' : '') }}"/>
</label>
</div>
<input class="btn btn-success" type="submit" value="Submit">
</form>
Controller
public function store(Request $request)
{
$category = new Category();
$category->category_is_menu = $request->category_is_menu;
return $category;
}
Unfortunately, it's giving me NULL for category_is_menu.
Add a hidden input with the negative value just before the checkbox input (with the same value in the name attribute)
<div class="form-group">
<input type="hidden" name="category_is_menu" value="0"/>
<input type="checkbox" name="category_is_menu" value="1" {{ old('category_is_menu', isset($category) ? 'checked' : '') }}/>
</div>
The value in the checkbox input needs to be always 1.
A checkbox input in not sent in the request when it's unchecked, in that case the hidden input will be sent with the value 0.
When the Checkox is checked, it will overwrite the value to 1.
Either use the hidden input trick as mentioned in the other answers or just check if the input was passed at all. If it was passed at all it was checked, if its not there it was not checked:
$category->category_is_menu = $request->has('category_is_menu');
If it was checked you get true, 1, if it wasn't checked you get false, 0.

Deleting record in Laravel

Below is my code which I have written to delete data from MySql using laravel but I am facing a problem while deleting; which is it always delete the top most row; regardless of which row I clicked.
Edit
<a href="#" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form').submit();
}
">
Delete
<form id="delete-form" action="{{ route('posts.destroy',[$post->id]) }}" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>
Here is my controller file:
public function destroy(Post $post)
{
dd($post->id); same id comes here always rregardless of which row I might click...
$findpost = Post::find($post->id);
if($findpost->delete()){
return redirect('/posts')->with('success','Post Removed Successfully') ;
}
return back()->withinput()->with('error',"Company Post be deleted");
}
It's because of you define same ID for all your fomrs.
You can define unique form id by adding POST ID at the end of it.
So, your code would be:
Edit
<a href="#" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form{{$post->id}}').submit();
}
">
Delete
<form id="delete-form{{$post->id}}" action="{{ route('posts.destroy',[$post->id]) }}" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>
The problem you are facing is because you are referencing to the same form every single time. ID in HTML is unique for an element, and it will always reference to one element randomly in your case always the first element with that ID on the page.
A better approach would be to have the url for each element in the anchor tag:
<a href="{{ route('posts.destroy',[$post->id]) }}" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form').setAttribute("action", this.href).submit();
}
return false; // to prevent submitting the form on click before the alert is displayed
">
and have one <form> element in your view:
<form id="delete-form" action="" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>

Spring + Thymeleaf - Call a function using received parameter

I have a table filled with a List of String that I receive from a controller, and then I want to put a button for each one to call to another function and get some objects related with that string.
<tbody th:each="titulo : ${listaColecciones}">
<tr>
<th th:utext="${titulo}"></th>
<th>
<form class="navbar-form navbar-left" action="#" th:action="#{/twittercontrolador/recuperarColeccion}" th:object="${textocoleccion}" th:value="${titulo}" method="post">
<button type="submit" class="btn btn-primary" value="Filtrar">Recuperar coleccion</button>
</form>
</th>
</tr>
</tbody>
But it seems not to work, it doesn't get the ${titulo} as a parameter for the function
Edit: Here I have a picture of what I'm trying to do:
As you can see, I get a List (Thre're database table names) in the controller from a method1, and I pass that list to the view.
There, I'm trying to put a table with 2 columns, first is the string/table name, and second is a button to call a second method which will return the objects in that table.
So, as you may suppose, the <tbody th:each="titulo : ${listaColecciones}">is the list of database table names.
<th th:utext="${titulo}"></th>
The names to know which table are you getting from the database
<form class="navbar-form navbar-left" action="#" th:action="#{/twittercontrolador/recuperarColeccion}" th:object="${textocoleccion}" th:value="${titulo}" method="post">
<button type="submit" class="btn btn-primary" value="Filtrar">Recuperar coleccion</button>
</form>
And here is where I'm getting the problems, the button. th:action="#{/twittercontrolador/recuperarColeccion}" is the second method in the controller and I don't know how to pass it the string (${titulo}) as a parameter for it.
Note that th:object="${textocoleccion}"is the name of the string I will receive in the second method but I can not set it to the value of the strings.
For all those who may have the same problem, this works for me:
<form class="navbar-form navbar-left" action="#" th:action="#{/twittercontrolador/recuperarColeccion}" th:object="${textocoleccion}" method="post" >
<button class="btn btn-success" type="submit" id="textocoleccion" name="textocoleccion" th:value="${titulo}">RECUPERAR</button>
</form>
I guess the key is to use id and name tags :D
Thanks to all
If you want to pass informations about the button you have clicked, then you should use input or button as tag and (that's important) a name and value attribute.
<input type="submit" name="somePostParamName" th:value="${titulo}" />

how to handle dynamically created forms laravel 5.4

I want to know how to fetch name of checkboxes from dynamically created form. First I am displaying each records of data in a separate form then If records need to be deleted we will be able to delete. I am able to give different name as somename+id. But how do I know which name is going in the controller. As It is not clear that which name is going in the controller, I am not able to delete the record. I am doing it in laravel 5.4. Here is my code -
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td>
<span class=""><input type="checkbox" name="deletecolor[{{$color->id}}]" value="{{$color->id}}"></span>
</td>
<td>
<div style="background:{{$color->web_color}}">a</div>
</td>
<td>{{$color->color_name}}</td>
<td>
<button type="submit" class="btn btn-danger">
Delete
</button>
</td>
</form>
</tr>
#endforeach
#endif
then on form submission I want to fetch which I am doing like this -
public function destroy(Request $request)
{
//
$id = $request->input('deletecolor');
$affected = DB::update("DELETE FROM vehicle_color where id = ?", [$id]);
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
I see you have $color->id. Why not delete them based on that?
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td><span class=""><input type="checkbox" name="deletecolor[{{ $color->id }}]" value="{{$color->id}}"></span></td>
<td><div style="background:{{$color->web_color}}">a</div></td>
<td>{{$color->color_name}}</td>
<td><button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#colorDelPopup">Delete</button></td>
</form>
</tr>
#endforeach
#endif
And in your controller:
public function destroy(Request $request)
{
$ids = $request->input('deletecolor');
// Instead of raw SQL, you can use the query builder to make your life a bit easier
$affected = DB::table('vehicle_color')->whereIn('id', $ids)->delete();
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
This should do the trick.

Resources