How get Multiple Checkbox value in Controller submitted from the form in laravel? - laravel

I have the table with each row containing checkbox where checkbox value is set as id from the database. How can i access them to controller to update in database. I have tried to dump the value in my controller but it show NULL.
Here is my view:
<form action= "po" method="POST" >
#csrf
<input type="submit" name="submit">
<div class="table-responsive">
<table class="table custom-table">
<thead>
<tr>
<th scope="col">
<label class="control control--checkbox">
<input type="checkbox" class="js-check-all"/>
<div class="control__indicator"></div>
</label>
</th>
<th scope="col" >S.N</th>
<th scope="col">LC NO</th>
<th scope="col">Applicant</th>
<th scope="col">Doc Value</th>
<th scope="col">Doc Received date</th>
<th scope="col">LC Type</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php $number = 1;?>
#foreach($datas as $items)
<tr>
<th scope="row" style="padding:20px">
<label class="control control--checkbox">
<input type="checkbox" name="checkboxlist[]" value="{{$items->id}}" />
<div class="control__indicator"></div>
</label>
</th>
<td>{{$number}}</td>
<td>{{$items->lc_no}}</td>
<td>{{$items->applicant}}</td>
<td>{{$items->doc_value}}</td>
<td>{{$items->rec_date}}</td>
<td>{{$items->sight_usance}}</td>
</tr>
<?php $number++; ?>
#endforeach
</tbody>
</table>
</div>
</form>
Here is my Controller
function po(Request $req){
dd($req->chekboxlist);
}

You are accessing wrong key from Request $req->chekboxlist
But it should be
$req->checkboxlist
Always add Validation to Avoid erorrs while saving data
$validate=Validator::make($request->all(), [
"checkboxlist" => "required|array",
]);
if($validate->fails()){
return redirect()->back()->with("errors",$validate->errors());
}

Related

laravel multiple input insert

I could not add more than one input value to the database. When the color is selected, the size numbers are listed, I want to save the piece value to these size numbers.
How can I record each input separately?
order_piece table
id|order_id|collection_color_size_barcode_id|piece
HTML code
<div class="table-scrollable" style="font-size: 14px;">
<table class="table">
<thead>
<tr>
<th> size </th>
<th> Barcode </th>
<th> order piece </th>
</tr>
</thead>
<tbody>
#foreach ($collection_color_size_barcode as $collection_color_size_barcode){
<tr>
<td><label class="col-md-3 control-label" style="margin-top: 5px;">{{$collection_color_size_barcode->size}}</label></td>
<td><label class="col-md-3 control-label" style="margin-top: 5px;">{{$collection_color_size_barcode->barcode}}</label></td>
<td>
<div class="col-md-5">
<input type="text" class="form-control input-sm" name="size[][{{$collection_color_size_barcode->id}}][piece]">
</div>
</td>
</tr>
}
</tbody>
</table>
Contoller code
public function OrderAdd(Request $request){
return dd($request->size);
}

Laravel Livewire checkbox checked on edit page

I'm facing an issue with checking the checkbox on the edit page using Livewire. I've tried many ways but still not able to check the old selected value
View Code:
<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds.{{$property->id}}"
value="{{$property->id}}" #if(in_array($property->id,$propertyIds)) checked #endif>
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>
Backend Code:
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}```
updated
And can you check if this correct? wire:model="propertyIds.{{$property->id}}"? Try to delete the wire:model .
<input class="branchCheckbox" type="checkbox"
value="{{$property->id}}"
#if(in_array($property->id,$propertyIds)) checked #endif>
`<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds"
value="{{$property->id}}" >
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>`
BACKEND
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}

passing a query result received from a controler in blade, back to another controller in laravel 8

I have a view that received a query result $necps from a controller, and I want to send to another controler with a form like below.
I am using a foreach to create the hidden inputs, but in the controller I get just the last record.
First question is : can I do that in a more elegant way? Sendind the entire query result at once?
Second : If not, how can I send all the array using foreach?
No Js please. Thanks a lot.
#if (isset($necps))
<div style="float:left; margin-left:20px;" >
<form style="display:inline;" action="{{route('mostra_varios_parts')}}" method="post">
#csrf
<button type="submit" class="btn btn-sm btn-mapa bi-globe texto_p"> Mapa</button>
#foreach($necps as $necp)
<input value="{{$necp->id_part}}" name="parts[id]" type="hidden">
<input value="{{$necp->latitude}}" name="parts[latitude]" type="hidden">
<input value="{{$necp->longitude}}" name="parts[longitude]" type="hidden">
<input value="{{$necp->nome_part}}" name="parts[nome_part]" type="hidden">
<input value="{{$necp->endereco}}" name="parts[endereco]" type="hidden">
#endforeach
</form>
</div>
<br>
<br>
<table class="table table-sm tabela-necessidade">
<thead>
<tr>
<th scope="col" class="texto_p">Nome</th>
<th scope="col" class="texto_p">Necessidade</th>
<th scope="col" class="texto_p">Categoria</th>
<th scope="col" class="texto_p">Data</th>
<th scope="col" class="texto_p">Quant</th>
<th scope="col" class="texto_p">Unidade</th>
<th scope="col" class="texto_p">Local</th>
<th scope="col" class="texto_p">Observações</th>
<!--<th scope="col" class="texto_p">Distância/Kms</th>-->
<th class="texto_p" colspan="1">Ações</th>
</tr>
</thead>
<tbody>
#if (count($necps)>0)
#foreach($necps as $necp)
<div>
<tr>
<td class="texto_p">{{$necp->nome_part}}</td>
<td class="texto_p">{{$necp->desc_nec}}</td>
<td class="texto_p">{{$necp->desc_cat}}</td>
<td class="texto_p">{{$necp->data}}</td>
<td class="texto_p">{{$necp->quant}}</td>
<td class="texto_p">{{$necp->desc_unid}}</td>
<td class="texto_p">{{$necp->endereco}} - {{$necp->cidade}}</td>
<td class="texto_p">{{$necp->obs}}</td>
<!--<td class="texto_p">{{$necp->distancia}}</td>-->
<td>
<form action="" method="post">
#csrf
<button type="submit" class="btn btn-sm btn-conectar bi-arrow-repeat texto_p"> Conectar</button>
<input value="{{$part->id}}" name="id_part_t" type="hidden">
<input value="{{$necp->id_nec_part}}" name="id_nec_part_t" type="hidden">
</form>
</td>
</tr>
</div>
#endforeach
#else
<td><td>Nenhum registro encontrado</td></td>
#endif
</tbody>
</table>
#endif
The name attribute for your hidden inputs need to be unique. you can post the fields as an array like so:
#foreach($necps as $necp)
<input value="{{$necp->id_part}}" name="parts[{{ $loop->index }}][id]" type="hidden">
<input value="{{$necp->latitude}}" name="parts[{{ $loop->index }}][latitude]" type="hidden">
<input value="{{$necp->longitude}}" name="parts[{{ $loop->index }}][longitude]" type="hidden">
<input value="{{$necp->nome_part}}" name="parts[{{ $loop->index }}][nome_part]" type="hidden">
<input value="{{$necp->endereco}}" name="parts[{{ $loop->index }}][endereco]" type="hidden">
#endforeach
with $loop->index as array key you post 1 array for every $necp

How to redirect back to previous page after post request in laravel?

I have a system where user enters the date range from and to and gets all required data between that date range. Data retrieved from the database shows in the table containing checkbox to each row. When user Check the check box and submit the data it updates to the database, but I'm have to redirect to the same table after updating the data in database. It says the GET method is not supported for this route. Supported methods: POST. Need Help.
Here is my Date range fill view.
#extends('master')
#section('content')
<div class="input-group" style="margin:50px">
<table>
<form action="/payment_list_table" method="POST">
#csrf
<div class="form-outline">
<tr>
<th>
<label>From</label>
</th>
<th>
<input type="date" name= 'from' class="form-control" placeholder="Doc Received Date" required="" />
</th>
<th>
<label>To</label>
</th>
<th>
<input type="date" name= 'to' class="form-control" placeholder="Doc Received Date" required="" />
</th>
</tr>
<th>
<button type="submit" class="btn btn-success">
<i class="fas fa-search"></i>
</button>
</th>
</div>
</form>
</table>
</div>
#endsection
Here is my Search Table View
#extends('master')
#section('content')
<div class='container' style="margin-top:50px">
<div class="row">
<div class="input-group" style="margin:20px">
<form>
<table style="float:right">
<th>
<div class="form-outline">
<input type="search" id="form1" class="form-control" placeholder="Search"/>
</th>
</form>
</div>
<form method="POST">
#csrf
<button type="submit" formaction="#" name="submit" class="checklistpo">PO</button>
<button type="submit" formaction="#" name="submit" class="ajax.po">AO</button>
<div class="table-responsive">
<table class="table custom-table">
<thead>
<tr>
<th scope="col">
<label class="control control--checkbox">
<input type="checkbox" class="js-check-all"/>
<div class="control__indicator"></div>
</label>
</th>
<th scope="col" >S.N</th>
<th scope="col">LC NO</th>
<th scope="col">Applicant</th>
<th scope="col">Doc Value</th>
<th scope="col">Doc Received date</th>
<th scope="col">LC Type</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php $number = 1;?>
#foreach($datas as $items)
<tr>
<td><input type="checkbox" name="checkboxlist[]" value="{{$items->id}}" /></td>
<td>{{$number}}</td>
<td>{{$items->lc_no}}</td>
<td>{{$items->applicant}}</td>
<td>{{$items->doc_value}}</td>
<td>{{$items->rec_date}}</td>
<td>{{$items->sight_usance}}</td>
</tr>
<?php $number++; ?>
#endforeach
</tbody>
</table>
</div>
</form>
</div>
</div>
#endsection
Here is my Controller
function po(Request $req){
$validate=Validator::make($req->all(), [
"checkboxlist" => "required|array"
]);
foreach($req->checkboxlist as $list){
$data = Doc::find($list);
$data->payment_comment = "PO";
$data->Payment_status = "Prepared";
$data->save();
};
return Redirect::back();
if($validate->fails()){
return redirect()->back()->with("errors",$validate->errors());
}
}
And My Route
Route::post('po', [PaymentController::class, 'po']);
Your code needs a bit of formatting & make more understandable variables but anyways. You need to check if validation fails before foreach. return redirect()->back(); is the right way to go back.
function po(Request $req)
{
$validate = Validator::make($req->all(), [
"checkboxlist" => "required|array"
]);
if ($validate->fails()) {
return redirect()->back()->with("errors", $validate->errors());
}
foreach ($req->checkboxlist as $list) {
$data = Doc::find($list);
$data->payment_comment = "PO";
$data->Payment_status = "Prepared";
$data->save();
}
return redirect()->back();
}

MVC3 ASP Replace null value with empty space on the view

I have the following view which returns some text if the POnumber is null.
What I think I need to have instead of the if(Model.Invoice.PONumber == null) is a check mechanism ( maybe multiple if statements ) that will check the fields LineNumber, Description, UnitOfMeasure, QtyOrdered and if any of them is null it will replace it with N/A or empty space but it will still allow the user to see the rest of information available.
Do you have any sugestions? I am new to MVC and any help will be apreciated.
Thank you in advance for your time and help,Bobby
<div class="contentWrapper2">
<div class="content2">
<div class="clr lfl w100">
<h1>Invoice Detail</h1>
<div class="return-btn">
<a class="btn btnStyleC btn-back-invoice" href="#Url.Action("InvoiceHistory", "Account")">
Back to Invoice List</a>
</div>
</div>
#if (Model.ErpError.Length > 0)
{
<div class="clr lfl w100 error">
#Html.Raw(Model.ErpError)
</div>
}
else
{
if(Model.Invoice.PONumber == null)
{
<div class="lfl w100 clr messaging">
<p>No information available at the moment for current invoice.
Please call our sales department for further assistance.
</p>
</div>
}
else
{
<div class="clr lfl w100">
<div class="order-number-date">
<table>
<tr>
<th class="col-1">
<h3>Invoice #:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.InvoiceNumber</h3>
</td>
</tr>
<tr>
<th class="col-1">
<h3>Invoice Date:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.InvoiceDate.ToShortDateString()</h3>
</td>
</tr>
</table>
</div>
<div class="order-number-date">
<table>
<tr>
<th class="col-1">
<h3>Order #:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.OrderNumber</h3>
</td>
</tr>
<tr>
<th class="col-1">
<h3>PO #:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.PONumber</h3>
</td>
</tr>
<tr>
<th class="col-1">
<h3>Due Date:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.DueDate.ToShortDateString()</h3>
</td>
</tr>
</table>
</div>
</div>
<div class="clr lfl w100">
<div class="bill-ship">
<table>
<tr>
<th>
<h4>Billing Information</h4>
</th>
</tr>
<tr>
<td>#Model.Invoice.BTDisplayName
</td>
</tr>
<tr>
<td>
<#Html.Raw(Model.Invoice.BTAddress1)
</td>
</tr>
#if (!string.IsNullOrEmpty(Model.Invoice.BTAddress2))
{
<tr>
<td>#Html.Raw(Model.Invoice.BTAddress2)
</td>
</tr>
}
<tr>
<td>#Html.CityCommaStateZip(Model.Invoice.BTCity, Model.Invoice.BTState, Model.Invoice.BTZip)</td>
</tr>
<tr>
<td>#Model.Invoice.BTCountry
</td>
</tr>
<tr>
<td>#Model.Invoice.BTPhone1</td>
</tr>
<tr>
<td>#Model.Invoice.BTEmail
</td>
</tr>
</table>
</div>
</div>
if (Model.Invoice.InvoiceLines.Count > 0)
{
<div class="clr lfl w100 line-item-detail">
<table class="info-tbl">
<tr>
<th class="vid-item">Item #</th>
<th class="vid-desc">Description</th>
<th class="vid-um">
U/M
</th>
<th class="vid-qty">
Qty
</th>
<th class="vid-ship">
Ship Date
</th>
#if (Model.ShowPackslip)
{
<th class="vid-pack">Pack Slip</th>
}
<th class="vid-unit">Unit Price</th>
<th class="vid-ext">Ext Price</th>
</tr>
#foreach (var invoiceLine in Model.Invoice.InvoiceLines)
{
<tr>
<td class="vid-line">#invoiceLine.LineNumber</td>
<td class="vid-desc">#invoiceLine.Description</td>
<td class="vid-um">#invoiceLine.UnitOfMeasure</td>
<td class="vid-qty">#invoiceLine.QtyOrdered</td>
<td class="vid-ship">
#if (invoiceLine.ShipDate.ToShortDateString() == "1/1/0001")
{
}
else
{
#invoiceLine.ShipDate.ToShortDateString()
}
</td>
#if (Model.ShowPackslip)
{
<td class="vid-pack">
#invoiceLine.PackSlip
</td>
}
<td class="vid-unit">#invoiceLine.UnitPrice.ToCurrency()
</td>
<td class="vid-ext">#invoiceLine.ExtendedPrice.ToCurrency()
</td>
</tr>
}
</table>
</div>
}
<div class="clr lfl w100">
<table class="tbl-total">
<tr class="subtotal">
<th class="col-1">Subtotal</th>
<td class="col-2">#Model.Invoice.OrderSubTotal.ToCurrency()
</td>
</tr>
#if (Model.Invoice.DollarOffOrder > 0)
{
<tr>
<th class="col-1">Order Discount</th>
<td class="col-2">#Model.Invoice.DollarOffOrder.ToCurrency()</td>
</tr>
}
#if (Model.Invoice.ShippingAndHandling > 0)
{
<tr>
<th class="col-1">Shipping</th>
<td class="col-2">#Model.Invoice.ShippingAndHandling.ToCurrency()
</td>
</tr>
}
#if (Model.Invoice.MiscCharges > 0)
{
<tr>
<th class="col-1">Misc. Charges</th>
<td class="col-2">#Model.Invoice.MiscCharges.ToCurrency()</td>
</tr>
}
<tr>
<th class="col-1">Sales Tax</th>
<td class="col-2">#Model.Invoice.TotalTax.ToCurrency()</td>
</tr>
<tr>
<th class="col-1">Invoice Total</th>
<td class="col-2">#Model.Invoice.InvoiceTotal.ToCurrency()</td>
</tr>
</table>
</div>
<div class="clr lfl w100">
<a class="btn btnStyleB btn-print" href="javascript:window.print();">Print</a>
</div>
}
}
</div>
</div>
You could create a template called for example "nullcheck.cshtml" like:
#if (ViewBag.ValueToCheck == null) {
<div class="lfl w100 clr messaging">
<p>
No information available at the moment for #(ViewBag.Field).
Please call our sales department for further assistance.
</p>
</div>
}
else {
#Html.Partial(ViewBag.TargetTemplate, Model)
}
Then you call it from your main view:
#{
ViewBag.TargetTemplate = "okModel";
ViewBag.Field = "P.O.Number";
ViewBag.ValueToCheck = Model.Invoice.PONumber;
Html.RenderPartial("nullCheck", Model, ViewBag);
}
okModel.cshtml should be the part of your template you will display when the value is not null...
I haven't tested this myself but it should give you some ideas... contact me if things go wrong XD
Cheers!
This seems like something you should take care of in your controller.
public ActionResult YourControllerAction()
{
var myViewModel = SomeService.GetMyViewModel();
if (myViewModel.Invoice.PONumber == null)
{
myViewModel.Invoice.PONumber = "N/A";
}
//etc
}
This leaves your view clearer (my personal preference)
However in the view you could simply use the null coalescing operator like so:
#Model.Invoice.PONumber ?? "NA"

Resources