laravel, checkbox wont pass form - laravel

Why won't checkbox pass into Input? With other input types (text,password,select) it works fine.
form
<form method="POST" enctype="multipart/form-data" id="form">
#foreach ($query as $article)
<tr>
<td>{{ $article->title }}</td>
<td>{{ $article->slider }}</td>
<td><i class="fa fa-edit"></i> Edit</td>
<td><input type="checkbox" class="group-checkable" name="article[]" value="{{ $article->id }}"></td>
</tr>
<input name="test" value="tedt" hidden>
#endforeach
</form>
and controller
if (Request::isMethod('post'))
{
dd(Input::get('article'));
}

Related

How to get Laravel Blade value as an Array data using Post Method?

I Have a Blade Form that will execute Post Method, this is my blade
#foreach ($dataku as $row => $order)
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12 text-center">
<b>{{ $order->delivery_order_no }}</b>
<input type="hidden" name="order[{{ $row }}][do_id]" value="{{ $order->id }}">
<input type="hidden" name="order[{{ $row }}][so_id]" value="{{ $order->sales_order->id }}">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive m-t-40" style="clear:both;">
<table class="table table-hover" style="font-size: 9pt;">
<thead>
<tr><th class="text-center">No</th>
<th class="text-center">SKUID</th>
<th class="text-center">Item Name</th>
<th class="text-center">UOM</th>
<th class="text-center">Qty So</th>
<th class="text-center">Qty Do</th>
<th class="text-center">Qty Confirm</th>
<th class="text-center">Qty Minus</th>
<th class="text-center">Remark Confirm</th>
</tr>
</thead>
<tbody>
#foreach ($order->delivery_order_details as $do =>$detOrder )
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $detOrder->skuid }}</td>
<td>{{ $detOrder->sales_order_detail->item_name }}</td>
<td>{{ $detOrder->uom->name }}</td>
<td>{{ $detOrder->sales_order_detail->qty }}</td>
<td>{{ $detOrder->qty_do }}</td>
<td>
<input type="hidden" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][skuid]]" value="{{ $detOrder->skuid}}">
<input type="number" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_do]]" value="{{ $detOrder->qty_do }}">
</td>
<td>
<input type="number" min="0" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_minus]]" value="0">
</td>
<td>
<input type="text" placeholder="Remark Confirm" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][remarks]]">
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endforeach
I Just want to get the data from the blade as an array ,... and then this is my controller
public function update(Request $request)
{
return $request->all();
}
I Get The Data Like This
is the return value from my blade $request->all(); correct? looks like something wrong ???
#sta provided an answer but I would make it more detailed:
When you build more complex structure (nested) in a html form input, every next key or list has to be surrounded by [].
So if you do just a nested keys it will be something[key1][key2][key3].
And if you do an array and a nested keys it will be something[key1][key2][][key3], where [] means that [key2] will be an array (and every array element will have a key3 key with provided value in input value attribute.
That's why this input name can't work:
order[0][detail[][0][skuid]]
but this one will work:
name="order[0][detail][0][qty_minus]

insert data through checkbox in laravel

I am trying to insert the checkbox data in database in which data are coming dynamically from database,
Here is the form :
<form action="{{ $action }}" method="post" enctype="multipart/form-data">
#csrf()
#if(!empty($method)) #method($method) #endif
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
#foreach($users as $k=>$u)
<tr>
<td><input type="checkbox" name="user_id[]" value="{{ $u->id }}"></td>
<td>{{ $u->name }}</td>
<td>{{ trim($evtitle) }}</td>
<td>{{ trim($evteacher) }}</td>
</tr>
#endforeach
</table>
<input type="text" name="question_id[]" value="{{ trim($evid) }}">
<input type="submit" class="btn btn-lg btn-success" value="Share" name="btn_btn_add_product">
</form>
Here is the controller :
$rs = ShareQuestion::create([
'user_id' => $request->input('user_id'),
'question_id' => $request->input('question_id')
]);
if($rs)
{
$message = array('flag'=>'alert-success', 'message'=>'Successfully');
return redirect()->route('auth.question.index')->with(['message'=>$message]);
}
$message = array('flag'=>'alert-danger', 'message'=>'Please try again');
return redirect()->route('auth.question.share')->with(['message'=>$message]);
Please help me out,
I think here is the issue as your checkbox is not checked so data is not passed to the controller function.
so change from(if you want to send all user_id)
<td><input type="checkbox" name="user_id[]" value="{{ $u->id }}"></td>
to
<td><input type="checkbox" name="user_id[]" checked value="{{ $u->id }}"></td>
or you can pass the condition for a checked attribute by
{{ Form::checkbox('user_id[]',$u->id,true, array('class'=>'some_class')) }}
$request->user_id and $request->question_id, array of values you have to use json_encode() method
$rs = ShareQuestion::create([
'user_id' => json_encode($request->user_id),
'question_id' => json_encode($request->question_id)
]);

Uploading image can't saved at directory and having error "htmlspecialchars() expects parameter 1 to be string, array given "

i have a view input like that :
<form class="form-group" action="/user6" method="post" enctype="multipart/form-data">
<table class="table table-striped">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th style="width: 60px">Tidak Baik</th>
<th style="width: 60px">Baik</th>
</tr>
<div class="form-group">
<tr>
<td>1.</td>
<td><input class="form-control" style="border:none" type="text" name="question1" value="Kondisi, kebersihan, pelumasan bearing" readonly></td>
{{-- <td><input class="form-control" type="text" placeholder=".input-lg"></td> --}}
<td>
<label><input type="radio" name="answer1" value="tidak baik" checked></label>
</td>
<td>
<label><input type="radio" name="answer1" value="baik"></label>
</td>
</tr>
<tr>
<td></td>
<td> <input class="form-control" style="border:none" type="file" name="image" value="" readonly> </td>
</tr>
</div>
<tr>
<td></td>
<td> <div class="form-group">
<label>Catatan</label>
<textarea class="form-control" name="catatan" rows="3" placeholder="Enter ..." required></textarea>
</div>
</tr>
<input type="hidden" name="alat_id" value="7">
<input type="hidden" name="status" value="3 Bulanan">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</tbody>
</table>
<div class="box-footer">
<button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
</div>
</form>
and to save this i have controller :
public function store6(Request $request)
{
if($request->hasfile('image'))
{
foreach($request->file('image') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan']));
$user->catatan = $request->catatan;
$user->status = $request->status;
$user->save();
//dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
before i add "enctype="multipart/form-data" , my view dont have error BUT cant display and saved image at directory . i want saved image to directory but cant saved .
i create a folder named 'images' at public . after i add enctype="multipart/form-data" . this view having error "htmlspecialchars() expects parameter 1 to be string, array given "
its my view after input :
<table class="table table-condensed">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
{{-- <th style="width: 40px">Label</th> --}}
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['answer1'] }}
</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['image'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['image'] }}
</td>
</tr>
<td><img src="{{ url('images/'.$pemeliharaan->pertanyaan['image'])}}"></td>
</tbody></table>
Something in your table is array, it's not a string so when you put it inside {{}}, it will show error.
You should check
$pemeliharaan->pertanyaan['question1']
$pemeliharaan->pertanyaan['answer1']
$pemeliharaan->pertanyaan['image']
by using dd() function.
Deal with one problem at a time. At present you are storing the image but not saving the path to the file, which you saved in $data. You are not using $data anywhere.
Then you have a problem rendering the new model in the view. This is a totally different issue and is caused by having non-string data passed to the htmlspecialchars function which is what {{ }} does.

Delete function in view | Laravel

I have this function to delete only a row from a table in my database.
public function delete($id)
{
DB::table('user')->where('userID', '=', $id)->delete();
return redirect('userAdmin');
}
And I have a button which gets created for every row.
#foreach ($scores as $score)
<tr>
<td>{{ $score->id }}</td>
<td>{{ $score->serialnumber }}</td>
<td>{{ $score->name }}</td>
<td>{{ $score->created_at }}</td>
<td></td>
<td>
<button class="btn btn-danger" type="submit">Delete this Row</button>
{{ csrf_field() }}
</td>
</tr>
How do I get a delete functionality behind it?
In your view:
<form action="{{ route('yourmodel.delete', $score->id) }}" method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Add a route with a name you like, and a parameter for the id. Let that route call the method in your example and you should be good.
There isn't really such a thing as a DELETE function in HTML forms, so you have to spoof it. Take a look at the docs.
In your view:
<form action="{{ route('yourmodel.delete', $score->id) }}" method="post">
{{ csrf_field() }}
#foreach ($scores as $score)
<tr>
<td>{{ $score->id }}</td>
<td>{{ $score->serialnumber }}</td>
<td>{{ $score->name }}</td>
<td>{{ $score->created_at }}</td>
<td></td>
<td>
<button class="btn btn-danger" type="submit">Delete this Row</button>
</td>
#endforeach
</form>
In your routes:
Route::post('Test/{id}', 'TestController#delete')->name('yourmodel.delete');

Multiple delete in Laravel

I'm currently trying to do multiple deletion in Laravel 5.3. I'd like to be something just like how emails are deleted using checkbox.
But before deleting, I'd like to do confirmation using a modal.
form in my blade looks like this
<form id="linkForm" action="/links/deleteLinks" method="POST">
{{ csrf_field() }}
{{ method_field('POST') }}
<div class="ibox-content">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover dataTables-example footable toggle-arrow-tiny">
<div class="btn-group btn-group-sm pull-right">
<a data-toggle="modal" class="btn btn-primary" href="#modal-form-add" data-dismiss="modal"><i class="fa fa-plus-square-o"></i> Add</a>
<input class="btn btn-danger" type="submit" value="Delete">
</div>
<thead>
<tr>
<th></th>
<th data-toggle="true" data-hide="all">Company</th>
{{-- <th>Category</th> --}}
<th>Page Url</th>
<th data-hide="all">Domain Url</th>
<th>Destination Url</th>
<th>Contact Email</th>
<th data-hide="all">Trust Flow</th>
<th data-hide="all">Estimated Traffic</th>
<th data-hide="all">Domain Authority</th>
<th>Status</th>
<th>Note</th>
<th data-hide="all">Live</th>
<th>Action</th>
{{-- <th>Delete</th> --}}
</tr>
</thead>
<tbody>
#foreach($links as $link)
<tr>
<td align="center"><input type="checkbox" class="i-checks" id="input" name="input[]" value="{{ $link->page_url }}"></td>
<td>{{ $link->company->name }}</td>
{{-- <td>{{ $link->category_id }}</td> --}}
<td>{{ $link->page_url }}</td>
<td>{{ $link->domain_url }}</td>
<td>{{ $link->destination_url }}</td>
<td>{{ $link->contact_email }}</td>
<td>{{ $link->trust_flow}}</td>
<td>{{ $link->estimated_traffic }}</td>
<td>{{ $link->domain_authority }}</td>
<td>{{ $link->status }}</td>
<td>{{ $link->comment }}</td>
<td>
#if($link->is_live)
{{ 'Live' }}
#else
{{ 'Down' }}
#endif
</td>
<td>
<a data-toggle="modal" href="#modal-form-{{ $link->id }}" data-dismiss="modal"><i class="fa fa-pencil"></i> Edit</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</form>
modal.blade.php
<div id="myModal" class="modal inmodal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body text-center">
<i class="fa fa-exclamation-triangle modal-icon"></i>
<h2><strong>Are you sure?</strong></h2>
{{-- <p class="text-muted">Are you sure you want to delete the following link(s)?</p> --}}
<p id="checkid"></p>
<div class="row">
<button id="cancelDelete" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn-primary btn" id="SubForm">Confirm and Submit The Form</button>
</div>
</div>
</div>
</div>
javascript
$("#linkForm").validate({
submitHandler: function (form) {
$("#myModal").modal('show');
$('#SubForm').click(function () {
form.submit();
});
}
});
How can I submit my array to the controller without creating another function and route for multiple delete and just use links.destroy?
You can pass the array from the form like this
<input type="checkbox" name="ids[]" value="{{$row->id}}"/>
In your controller you can do
Model::whereIn('id', $request->ids)->destroy();
You can always collect the links through JS and send them along. In the Laravel controller you'll simply add an if condition to differentiate between the two requests.
By the by, whether it is only one id or several, you should always make sure that permissions over them are enforced.
Elaborating on my answer,
Suppose you've got a collection of articles, each of which can be marked for deletion
<input type="checkbox" name="article[]" value="1" checked>
<input type="checkbox" name="article[]" value="2" checked>
<input type="checkbox" name="article[]" value="3" checked>
<input type="checkbox" name="article[]" value="4">
Then you can collect those ids with
let ids = Array.from(document.querySelectorAll('input[name="article[]"'))
.filter(el => el.checked)
.map(el => el.value);
And later on send them thorugh an XMLHttpRequest

Resources