I have a table with its data are retrieved from database here: https://imgur.com/Sv4Suo7 . My problem is, I want to delete the data that are selected in the checkboxes.
I have tried putting name="ids[]" in my checkbox, but the data is still not sent to my controller. I have read somewhere that I need to use Javascript, but I don't know how to.
Views:
<div class="box-header with-border">
<div class="box-header-tools pull-left" >
<a href="{{ url('tasks/create/')}}" class="tips text-green" title="{{ Lang::get('core.btn_create') }} ">
<i class="fa fa-plus-square-o fa-2x"></i></a>
<a href="{{ url('tasks/massDelete')}}" onclick="" class="tips text-red" title="{{ Lang::get('core.btn_remove') }}">
<i class="fa fa-trash-o fa-2x delete_all" data-toggle="confirmation" data-title="{{Lang::get('core.rusure')}}" data-content="{{ Lang::get('core.rusuredelete') }}" ></i></a>
</div>
</div>
<div class="box-body" >
<div class="table-responsive" style="min-height:300px; padding-bottom:60px; border: none !important">
<table class="table table-striped table-bordered " id="{{ $pageModule }}Table">
<thead>
<tr>
<th align="center" class="number"> No </th>
<th align="center"> <input type="checkbox" class="checkall" id="master" /></th>
<th align="center">Task</th>
<th align="center">Due Date</th>
<th align="center">Assigned To</th>
<th align="center">Assigned By</th>
<th align="center">Status</th>
<th align="center">{{ Lang::get('core.btn_action') }}</th>
</tr>
</thead>
<tbody> #foreach($tasks as $task)
<tr>
<td width="30"> {{ ++$i }} </td>
<td width="50"><input type="checkbox" class="checkbox" name="ids[]" value="{{$task->id}}" /></td>
<td>{{$task->task_name}} </td>
<td>{{$task->due_date}}</td>
#foreach($users as $user)
#if($user->id == $task->assigned_id)<td>{{$user->username}}</td>#endif
#endforeach
#foreach($users as $user)
#if($user->id == $task->assigner_id)<td>{{$user->username}}</td>#endif
#endforeach
#if($task->status == 0)<td width="90">
<span class="label label-block label-info label-sm">Ongoing</span>
</td>#endif
#if($task->status == 1)<td width="90">
<span class="label label-block label-danger label-sm">Cancelled</span>
</td>#endif
#if($task->status == 2)<td width="90">
<span class="label label-block label-success label-sm">Completed</span>
</td>#endif
<td>
#if($task->status == 0)
{!! Form::open(array('url'=>'tasks/completeStatus/'.$task->id, 'class'=>'form-horizontal')) !!}
<button type="submit" name="markcomplete" class="btn" ><i class="fa fa-check-circle-o fa-2x"></i></button>
{!! Form::close() !!}
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
<input type="hidden" name="md" value="" />
</div>
</div>
</div>
</div>
Controller:
public function massDelete(Request $request)
{
$gotids = $request->input('ids');
if($gotids){
foreach($gotids as $id){
$task = Tasks::findOrFail($id);
$task->delete();
}
}
return redirect('tasks');
}
Route:
Route::get('/tasks/massDelete/', 'TasksController#massDelete');
I wanted the data to be in controller, when I tried dd($gotids); it displays null. Hope anyone can help.
Here is the code if you want to use javascript
Route::delete('delete-multiple-category', ['as'=>'category.multiple-delete','uses'=>'CategoryController#deleteMultiple']);
public function deleteMultiple(Request $request){
$ids = $request->ids;
Category::whereIn('id',explode(",",$ids))->delete();
return response()->json(['status'=>true,'message'=>"Category deleted successfully."]);
}
blade file
<div class="container">
<h3>PHP Laravel 5.6 - How to delete multiple row with checkbox using Ajax? - HDTuto.com</h3>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<button style="margin: 5px;" class="btn btn-danger btn-xs delete-all" data-url="">Delete All</button>
<table class="table table-bordered">
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>S.No.</th>
<th>Category Name</th>
<th>Category Details</th>
<th width="100px">Action</th>
</tr>
#if($categories->count())
#foreach($categories as $key => $category)
<tr id="tr_{{$category->id}}">
<td><input type="checkbox" class="checkbox" data-id="{{$category->id}}"></td>
<td>{{ ++$key }}</td>
<td>{{ $category->category_name }}</td>
<td>{{ $category->category_details }}</td>
<td>
{!! Form::open(['method' => 'DELETE','route' => ['category.destroy', $category->id],'style'=>'display:inline']) !!}
{!! Form::button('Delete', ['class' => 'btn btn-danger btn-xs','data-toggle'=>'confirmation','data-placement'=>'left']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#endif
</table>
<script type="text/javascript">
$(document).ready(function () {
$('#check_all').on('click', function(e) {
if($(this).is(':checked',true))
{
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked',false);
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#check_all').prop('checked',true);
}else{
$('#check_all').prop('checked',false);
}
});
$('.delete-all').on('click', function(e) {
var idsArr = [];
$(".checkbox:checked").each(function() {
idsArr.push($(this).attr('data-id'));
});
if(idsArr.length <=0)
{
alert("Please select atleast one record to delete.");
} else {
if(confirm("Are you sure, you want to delete the selected categories?")){
var strIds = idsArr.join(",");
$.ajax({
url: "{{ route('category.multiple-delete') }}",
type: 'DELETE',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: 'ids='+strIds,
success: function (data) {
if (data['status']==true) {
$(".checkbox:checked").each(function() {
$(this).parents("tr").remove();
});
alert(data['message']);
} else {
alert('Whoops Something went wrong!!');
}
},
error: function (data) {
alert(data.responseText);
}
});
}
}
});
$('[data-toggle=confirmation]').confirmation({
rootSelector: '[data-toggle=confirmation]',
onConfirm: function (event, element) {
element.closest('form').submit();
}
});
});
</script>
Related
Please help to solve my this issue
my controller code is
public function index()
{
$callrecordings = DB::table('callrecordings')
->join('users', 'users.id', '=', 'callrecordings.user_id')
->leftJoin('disableapp', 'callrecordings.user_id', '=', 'disableapp.user_id')
->select('callrecordings.*', 'users.expiry_date')
->where('callrecordings.user_id', '=', Auth::id())
->where(function($q){
$q->where('disableapp.status', '=', 1)
->orWhereNull('disableapp.status');
})
->paginate(5);
dd($callrecordings);
if($callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString()){
$result = 'That email belongs to an existing referrer.';
return view('frontend.views.package-expire', compact('result'));
}
else{
return view('frontend.views.callrecordings', compact('callrecordings'));
}
}
my blade file is
<table id="example" class="table table-bordered dt-responsive nowrap dataTable no-footer dtr-inline" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Duration</th>
<th>Size</th>
<th>Direction</th>
<th>Audio</th>
<th>Download</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
#foreach ($callrecordings as $callrecording)
<tr>
<td>{{ $callrecording->name }}</td>
<td>{{ $callrecording->phone}}</td>
<td>{{ $callrecording->duration}}</td>
<td>{{ $callrecording->size}}</td>
<td>
#if ($callrecording->direction === 'Incoming')
<span class="label label-success">Incoming</span>
#elseif ($callrecording->direction === 'Outgoing')
<span class="label label-info">Outgoing</span>
#elseif ($callrecording->direction === 'Rejected')
<span class="label label-warning">Rejected</span>
#else
<span class="label label-danger">Missed</span>
#endif
</td>
<td>
<audio id='sound1'>
<source src="{{ URL::to('/') }}/uploads/audio/{{ $callrecording->audio }}" type="audio/ogg">
<source src="{{ URL::to('/') }}/uploads/audio/{{ $callrecording->audio }}" type="audio/mpeg">
</audio>
<div class="play btn btn-success btn-xs" id="btn1">play</div>
</td>
<td>
<i class="icon-download-alt"> </i> Download
<!---button class="btn btn-primary btn-xs" data-download="{{ URL::to('/') }}/uploads/audio/{{ $callrecording->audio }}"><i class="fa fa-fw fa-download"></i> Download</button---->
</td>
<td>{{ $callrecording->created_at }}</td>
</tr>
#endforeach
</tbody>
</table>
this condition is working but if I do table status 0 or no value-added in a table it gives this error message Trying to get property 'expiry_date' of non-object in.
I attached my dd($callrecordings);
Please tell me how Should I overcome from this issue
thanks in advance.
you missed something in your code which check 0 element for data if data not exist ,so we need to make check of existance for record
if(isset($callrecordings[0]->expiry_date) && $callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString()){
I have a featureds table (foreign key= product_id) that belongsto products table, Now i want to save some products id to my featured table,its give me a array reasult but i couldnot save it to database
Here is my Controller -->
public function featuredProduct(Request $request)
{
if($request->isMethod('POST')){
$product_id=$request->all();
foreach ($product_id as $product) {
$products[]=$product;
}
//dd($products);
Featured::create($products);
}
return view('admin.products.featured');
}
<form action="{{ route('featuredProduct') }}" method="POST" multiple>
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap verticle_middle">
<thead>
<tr>
<th>Product</th>
<th>Category</th>
<th>Image</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach ($products as $product)
<tr>
<td>
<input id="{{ $product->id }}" value="{{ $product->id }}" type="checkbox" name="product[]">
<label for=id={{ $product->id }}>{{ $product->product_name }}</label>
</td>
<td> {{ $product->category['cat_name'] }} </td>
<td> <img src="{{asset($product->pro_img) }}" alt="" width="40"> </td>
<td class="center">
#if ($product->status === 1)
<span class="btn btn-primary btn-xs">Published</span>
#else
<span class="btn btn-warning btn-xs">Unpublish</span>
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="modal-footer">
<button type="button"class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary waves-effect waves-light">Submit</button>
</div>
</form>
You can create each feature inside the for loop.
public function featuredProduct(Request $request)
{
if($request->isMethod('POST')) {
foreach($request->all() as $productId) {
Featured::create([
'product_id' => $productId,
... other data fields.
]);
}
}
}
Or if you want to save all the features at once.
public function featuredProduct(Request $request)
{
if($request->isMethod('POST')) {
$featureds = [];
foreach($request->all() as $productId) {
$featureds[] = [
'product_id' => $productId,
... other data fields.
]
}
DB::table('featureds')->insert($featureds);
}
}
I have a table with a jquery sortable plugin. You can drag and drop items and than save the new order. My question is, how do i disable columns of the table so i can just drag and drop by the Name column?
HTML code
<table class="table table-bordered" id="users" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Image</th>
<th>Spotify</th>
<th>Instagram</th>
<th>Soundcloud</th>
<th>Facebook</th>
<th>Website</th>
<th>Options</th>
</tr>
</thead>
<tbody id="sortable">
#if(count($rows) > 0)
#foreach($rows as $value)
<tr id="{{ $value->id }}">
<td>{{ $value->name }}</td>
<td>
<img src="{{ $value->getImage('s') }}" >
</td>
<td>{{ $value->spotify_username }}</td>
<td>{{ $value->instagram_username }}</td>
<td>{{ $value->soundcloud_username }}</td>
<td>{{ $value->facebook_username }}</td>
<td>{{ $value->website_url }}</td>
<td class="text-center text-white">
<a data-placement="bottom" title='Additional info about {{ $value->name }}' href='{{ route("artists.view", ["artist" => $value->id]) }}' class="btn btn-sm btn-info tooltip-custom">{{ __('Info') }}</a>
<a data-placement="bottom" title='Edit {{ $value->name }}' href='{{ route("artists.edit", ["artist" => $value->id]) }}' class="btn btn-sm btn-primary tooltip-custom">{{ __('Edit') }}</a>
<a data-placement="bottom" title='Delete {{ $value->name }}' data-name='{{ $value->name }}' data-toggle="modal" data-target="#deleteModal" data-href='{{ route("artists.delete", ["artist" => $value->id]) }}' class="btn btn-sm btn-danger tooltip-custom">{{ __('Delete') }}</a>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
Script code
$( function() {
$( "#sortable" ).sortable({
update: function (event, ui){
$("#input-new-order-state").val($('#sortable').sortable("toArray"));
$("#form-state").removeClass('d-none');
}
});
$( "#sortable" ).disableSelection();
});
In case someone needs this, i solved it.
I added "handle": ".enable-drag", to the script, and class="enable-drag" to the first td. Now you can drag and drop only by the first column.
$( function() {
$( "#sortable" ).sortable({
"handle": ".enable-drag",
update: function (event, ui){
$("#input-new-order-state").val($('#sortable').sortable("toArray"));
$("#form-state").removeClass('d-none');
}
});
$( "#sortable" ).disableSelection();
} );
<div class="table-responsive">
<table class="table table-bordered" id="users" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Image</th>
<th>Spotify</th>
<th>Instagram</th>
<th>Soundcloud</th>
<th>Facebook</th>
<th>Website</th>
<th>Options</th>
</tr>
</thead>
<tbody id="sortable">
#if(count($rows) > 0)
#foreach($rows as $value)
<tr id="{{ $value->id }}">
<td class="enable-drag">{{ $value->name }}</td>
<td>
<img src="{{ $value->getImage('s') }}" >
</td>
<td>{{ $value->spotify_username }}</td>
<td>{{ $value->instagram_username }}</td>
<td>{{ $value->soundcloud_username }}</td>
<td>{{ $value->facebook_username }}</td>
<td>{{ $value->website_url }}</td>
<td class="text-center text-white">
<a data-placement="bottom" title='Additional info about {{ $value->name }}' href='{{ route("artists.view", ["artist" => $value->id]) }}' class="btn btn-sm btn-info tooltip-custom">{{ __('Info') }}</a>
<a data-placement="bottom" title='Edit {{ $value->name }}' href='{{ route("artists.edit", ["artist" => $value->id]) }}' class="btn btn-sm btn-primary tooltip-custom">{{ __('Edit') }}</a>
<a data-placement="bottom" title='Delete {{ $value->name }}' data-name='{{ $value->name }}' data-toggle="modal" data-target="#deleteModal" data-href='{{ route("artists.delete", ["artist" => $value->id]) }}' class="btn btn-sm btn-danger tooltip-custom">{{ __('Delete') }}</a>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
when i load the page normally everything is going, the form is submitted and i get the right responses .. but when i load the page with .load() function jQuery the form doesnt submitted! and i get nothing from Ajax function!! any help
this is my HTML :
<div class="container">
<div class="row">
<h4>Manage Contents</h4>
<div class="table-responsive table3">
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Content</th>
<th>Owner</th>
<th>Created Date</th>
<th>Project</th>
<th>Priorty</th>
<th>Options</th>
</tr>
</thead>
<tbody>
#if ($availabe >= 1 )
<?php $i =1; ?>
#foreach ($avashown as $key)
<?php //this is edit content form?>
#if(isset($index) && $index == $i && isset($editid) && isset($idexist) && $idexist == $key->id)
<tr>
<form id="edit-content-form" action="/testajax" method="POST">
<td>{{ $i }}</td>
<td hidden="hidden">{{ csrf_field() }}</td>
<td hidden="hidden"><input type="hidden" name="id" value="{{ $editid }}"></td>
<td hidden="hidden"><input type="hidden" name="index" value="{{ $index }}"></td>
<td colspan="3">
<div class="control-group" id="inputField1">
<input class="form-control" type="text" name="content" value="{{ $key->content }}">
<span class="help-block hideme" id="help-block1"></span>
</div>
</td>
<td colspan="1">
<div class="control-group" id="inputField2">
<select class="form-control" name="project">
#foreach ($projects as $project)
<option value="{{ $project->id }}">{{ $project->projectname }}</option>
#endforeach
</select>
<span class="help-block hideme" id="help-block2"></span>
</div>
</td>
<td colspan="1">
<div class="control-group" id="inputField3">
<select class="form-control" name="priority">
#foreach ($priorities as $priority)
<option value="{{ $priority->id }}">{{ $priority->value }}</option>
#endforeach
</select>
<span class="help-block hideme" id="help-block3"></span>
</div>
</td>
<td>
<input type="submit" id="submitForm" name="submit" value="Done">
</td>
</form>
</tr>
<?php $i++;//i have to increase it here again?>
#else
<tr>
<td class="hidden"></td>
<td>{{ $i }}</td>
<td>{{ $key->content }}</td>
<?php $firstname = \App\User::find($key->owner_id);?>
<td>{{ ucfirst($firstname->firstname) }}</td>
<td>{{ $key->created_at }}</td>
<?php $projectname = \App\Project::find($key->project_id);?>
<td>{{ $projectname-> projectname }}</td>
<?php $priorty = \App\Priority::find($key->priority_id);?>
<td><span class ="label label-{{ $priorty->name }}">{{ $priorty->value }}<span></td>
<td>
<span class="glyphicon glyphicon-trash" data-toggle="tooltip" data-placement="bottom" title="Delete" onclick="ContentStatus('delete/ajax/{{ $key->id }}')"></span><span> </span>
<span id="editContentGlyph" data-toggle="tooltip" data-placement="bottom" title="Edit" class="glyphicon glyphicon-cog" onclick="editContent({{ $i }},{{ $key->id }})"></span>
</td>
<?php $i++; ?>
</tr>
#endif
#endforeach
#else
<tr>
<td>0</td>
<td colspan="6">Let's Work and Add some Contents! Create</td>
</tr>
#endif
</tbody>
</table>
</div>
</div>
</div>`
this is my script file :
function editContent(index,id){
var place = ".table3 tbody tr:nth-child("+index+") " ;
var place = "#alltables" ;
var des = place+" > *";
$(document).ready(function(){
$( place ).load("http://localhost:8000/manage_contents/"+index+"/"+id+" "+des, function(){
});
});
}
$(document).ready(function(){
$(function(){
$('#edit-content-form').on('submit',function(e){
$.ajaxSetup({
header:$('meta[name="_token"]').attr('content')
})
e.preventDefault(e);
$.ajax({
type:"PUT",
url:'http://localhost:8000/testajax',
data:$(this).serialize(),
dataType: 'json',
success: function(data){
$("#alerts").html(data.responseText);
if (data.updated) {
var place = ".table3 tbody tr:nth-child("+data.index+") " ;
var des = place+" > *";
$( place ).load("http://localhost:8000/manage_contents/ "+des, function(){
});
}
},
error: function(data,xhr, ajaxOptions, thrownError){
$.each(data.responseJSON, function(){
var i = 1;
$.each(this, function(index,error) {
$("#inputField"+i).addClass(" has-error");
$("#help-block"+i).removeClass(" hideme");
$("#help-block"+i).html("<strong>"+error+"</strong>");
i++;
});
});
},
})
});
});
});
`
I suspect its happening due to your HTML form is getting loaded via ajax and before that only your $(document).ready( code is getting executed. Hence at that time there will be no element with id edit-content-form present in DOM. So to deal with this situation try using event delegation technique like below:
$(function(){
$(document).on('submit','#edit-content-form',function(e){
// your existing code goes here
});
});
We are attaching the event listener to the document object which will be there for sure and then delegating to #edit-content-form whenever event occurs.
I want to search and display specific data from db and the method that I have made is not retrieving data from database, if i print it just gives a empty array.
My code is as follow:
Controller:
public function search() {
$search = Input::get('search');
if($search){
$query = DB::table('volunteer');
$results = $query ->where('bloodt', 'LIKE', $search) ->get();
//print_r($results); exit;
return View::make('search')->with("volunteer",$results);
}
}
View:
{{Form::open(array('method'=>'POST'))}}
<div class="pull-left col-xs-6 col-xs-offset-0">
{{Form::text('search','',array('placeholder'=>'Search by blood type/zip code/address....','class'=>'form-control','required autofocus'))}}
</div>
<input type="submit" name="search" value="Search" class="btn btn-primary">
<br><br>
{{Form::close()}}
<?php if (isset($results)) { ?>
#foreach($volunteer as $results)
<table class="table table-bordered" style="width: 100%; background-color: lightsteelblue">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>IC/Passport no.</th>
<th>Blood Type</th>
<th>Address</th>
<th>Zip Code</th>
<th>e-mail</th>
<th>Phone no.</th>
<th>Status</th>
</tr>
<tr>
<td>{{ $results->fname }}</td>
<td>{{ $results->lname }}</td>
<td>{{ $results->ic }}</td>
<td>{{ $results->bloodt }}</td>
<td>{{ $results->address }}</td>
<td>{{ $results->zipcode }}</td>
<td>{{ $results->email }}</td>
<td>{{ $results->phone }}</td>
<td>
<div class="btn-group" role="group">
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
{{ $results->status }}
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>Eligible</li>
<li>Not Eligible</li>
</ul>
</div>
</td>
</tr>
</table>
#endforeach
<?php;
} ?>
Route:
Route::post('search', 'HomeController#search');