Laravel Datatables while using Entrust Package - php

I'm using entrust package for roles management , i have impleted that in my project based on the steps which mentioned in github , it's working fine but for assigning roles to user page we are getting values from two queries which i mentioned below , if the user list goes upto 100 how can i get user details easily , i want datatables for this view can anyone please help me to view as datatables format and i already configured yajra datatables for my project , it is working for other pages, we are using https://github.com/Zizaco/entrust
Controller.php
public function index()
{
$company_id=Auth::user()->company_id;
$users=User::where('company_id',$company_id)->get();
$allRoles=Role::where('company_id',$company_id)->get();
return view('usersroles.index',compact(['users','allRoles']));
}
view.blade.php
<table class="table table-bordered" >
<tr class="thead-cls">
<th class="center">Name</th>
<th class="center">Employee Id</th>
<th class="center">Roles</th>
<th class="center">Action</th>
</tr>
#forelse($users as $user)
<tr>
<td class="center">{{$user->name}}</td>
<td class="center">{{$user->emp_id}}</td>
<td class="center">
#foreach( $user->roles as $role)
{{$role->name}},
#endforeach
</td>
<td class="center">
#permission('users-roles-edit')
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-add" data-toggle="modal" data-target="#myModal-{{$user->id}}">
Edit
</button>
#endrole
<!-- Modal -->
<div class="modal fade" id="myModal-{{$user->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"> Editing<b> {{$user->name}}'s</b> Role</h4>
</div>
<div class="modal-body">
<form action="{{route('usersroles.update',$user->id)}}" method="post" role="form" id="role-form-{{$user->id}}">
{{csrf_field()}}
{{method_field('PATCH')}}
<div class="form-group">
<select name="roles[]" multiple required="">
#foreach($allRoles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
#endforeach
</select>
</div>
{{--<button type="submit" class="btn btn-primary">Submit</button>--}}
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-add" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary btn-add" onclick="$('#role-form-{{$user->id}}').submit()">Save changes</button>
</div>
</div>
</div>
</div>
</td>
</tr>
#empty
<td>No users</td>
#endforelse
</table>
[![Need Datatables for this view][1]][1]

Just add this piece of code in your view with jQuery:
$(document).ready(function(){
$('#myTable').DataTable();
});
#myTable is the id of your table. For more info, read the documentation.

Related

Delete button not work bootstrap modal and Laravel

I created one table that list items, and I added two buttons(edit and delete) so, the button edit is working, but the button delete is not working, and I'm making me carzy because I don't find the error. this is the table:
<table id="datatable" class="table align-items-center table-flush">
<thead class="thead-light">
<tr>
<th>text</th>
<th class="disabled-sorting" width="10%"></th>
<th class="disabled-sorting" width="10%"></th>
</tr>
</thead>
<tbody>
#foreach($Items as $item)
<tr>
<td>{{ $item->text }}</td>
<td><a href="{{ route('ItemsController.edit', $item->id) }}" class="btn btn-primary btn-fab btn-icon btn-round" title="Edit">
<i class="fa fa-edit"></i></a>
</td>
<td>
<a href="#" class="btn btn-primary btn-fab btn-icon btn-round btn-delete" title="Delete" data-id="{{ $item->id }}" data-toggle="modal" data-target="#modal-default" data-route="{{ route('ItemsController.destroy', $item->id) }}" data-title="{{ $item->id }}">
<i class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
</table>
the modal is:
<div class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Delete?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<form class="" action="" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger" name="button">yes, delete</button>
</form>
</div>
</div>
</div>
</div>
javascript data:
<script>
$(document).on('click', '.btn-delete', function(){
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
})
</script>
and controller function destroy
public function destroy(Items $item) {
$item->delete();
return redirect()->route('items.index')->with('success', 'Deleted Success');
}
I checked it line by line, and I don't know what I'm wrong, please help me
Many times thanks for getting into this.
You need to trigger your modal from JS.
If it is bootstrap 4 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
$("#modal-default").modal();
If it is bootstrap 5 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
modal = new bootstrap.Modal(document.getElementById("modal-default"));
modal.show();

Im getting the wrong id on my view, im getting id number:1 always on my category id

So this is my code, its a Laravel .blade.php but anyway my problem is with this HTML / PHP code.
The problem is that when I press on any delete button I get the same id, I get cat->id = 1. when I press the 'delete' button on the laptop row, I should get in my delete confirmation the id number: 2, but I still get the number: 1 and its that way with any other row
<div class="">
<div class="box">
<div class="box-header">
<h3 class="box-title">All Categories</h3>
</div>
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Id</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
#foreach($categories as $cat)
<tr>
<td>{{$cat->nombre}}</td>
<td>{{$cat->id}}</td>
<td>
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete">Delete</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
For the modal im using the next code:
<div class="modal modal-danger fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-center" id="myModalLabel"><span>Delete Confirmation</span><</h4>
</div>
<form action="{{route('categories.destroy','test')}}" method="post">
{{method_field('delete')}}
{{csrf_field()}}
<div class="modal-body">
<p class="text-center">
<span>Are you sure you want to delete this {{ $cat->id}}?</span>
</p>
<input type="hidden" name="category_id" id="cat_id" value="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"><span>No, Cancel</span></button>
<button type="submit" class="btn btn-warning"><span>Yes, Delete</span></button>
</div>
</form>
</div>
</div>
I pressed the delete button on the Laptop row, and I get the id number:1 when I should get id number:2. this apply on every row
This is because everytime you click on the delete button it calls the same model with the same ID. Change you Button ID
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete-{{$cat->id}}">Delete</button>
And the modal ID as well
<div class="modal modal-danger fade" id="delete-{{$cat->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
NOTE: You might need to keep the modal box in foreach loop so that delete button is mapped to the correct modal box

Laravel perform Update/edit inside index view is it possible?

is it possible to use the index view to perform update/edit at the same time using modal form? i have index view with table displaying the data,and inside the table it has button named [edit and delete] now i want to perform edit/update once the edit button is click then modal form will come out..? but whenever is use the modal form it will show error like this image.
This is my Controller:
public function show_setup()
{
$batch=Batch::all();
return view('setup.show_batch',compact('batch'));
}
public function edit_batch(request $request)
{
$batch = Batch::find ($request->id);
$batch->batch_name = $request->batch_name;
$batch->save();
return redirect()->back();
}
My view
<form>
<div class="form-group">
<table class="table table-hover table-bordered" id="table">
<tr>
<th>Batch</th>
<th>Action</th>
</tr>
#foreach($batch as $bt)
<tr>
<td>{{$bt->batch_name}}</td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</table>
</div>
</form> <!-- Modal-->
<div id="edit_batch" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
<div role="document" class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 id="exampleModalLabel" class="modal-title">Edit Batch</h5>
<button type="button" data-dismiss="modal" aria-label="Close" class="close">
<span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<form action="setup/batch/edit" method="POST">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="form-group">
<label>School Year</label>
<input type="text" placeholder="School Year" name="batch_name" value="{{$batch->batch_name}}" class="form-control" autocomplete="off">
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-secondary">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!--End batch Modal-->
The problem is that you want to get the batch_name from the collection $batch you have to pass the data you want using the javaScript the data you need are the id and the batch_name
Here is an example :
<form>
<div class="form-group">
<table class="table table-hover table-bordered" id="table">
<tr>
<th>Batch</th>
<th>Action</th>
</tr>
#foreach($batch as $bt)
<tr>
<td>{{$bt->batch_name}}</td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</table>
</div>
</form>
<!-- Modal-->
<div id="edit_batch" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
<div role="document" class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 id="exampleModalLabel" class="modal-title">Edit Batch</h5>
<button type="button" data-dismiss="modal" aria-label="Close" class="close">
<span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<form action="setup/batch/edit" method="POST">
{{csrf_field()}}
{{ method_field('PUT') }}
<input id="batch_id" type="hidden" name="id">
<div class="form-group">
<label>School Year</label>
<input id="batch_name" type="text" placeholder="School Year" name="batch_name" class="form-control" autocomplete="off">
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-secondary">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!--End batch Modal-->
<script type="text/javascript">
$('.editBtn').on('click',function(e)
{
var link = $(this);
batch_id = link.data("id");
batch_name = link.data("batch_name");
$("#batch_id").val(batch_id);
$("#batch_name").val(batch_name);
});
</script>

Pass data to modal in the same page, Laravel

So i have this index page which has the list of task and inside the table is the title of task and the actions, what i'm trying to do is when i click the task title the modal will pop whereas in the modal is its body and content of the said task.. here is the code
controller
public function index()
{
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
return view('posts.index')->with('posts', $posts);
}
index page
#extends('layout.app')
#section('content')
<div class="container">
<div class="col-md-6" style="float:left;">
<h2>Todo List</h2>
<div class="table-hover">
<table class="table">
<thead class="thead-default">
<tr>
<th>Title</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(count($todos) > 0)
#foreach($todos as $todo)
<tr>
<th>{{$todo->title}}</th>
<td>{{$todo->status}}</td>
<td><a class="btn btn-sample btn-sm">Edit</a> <a class="btn btn-sample2 btn-sm">Delete<a></td>
</tr>
</tbody>
#endforeach
#else
<th><h2>Nothing to show</h2></th>
#endif
</table>
</div>
</div>
the modal below the same page
<div class="modal fade" id="exampleModal3" tabindex="-1" role="dialog" aria-labelledby="exampleModal3Label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModal3Label">View task</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h3>{{$todo->title}}</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
There are two methods to achieve this, either you repeat your modal with unique id with tr or records, or you create one modal, and onClick bring data from ajax or javascript, and fill it within modal.
I'm explaining you the first and easiest one
#extends('layout.app')
#section('content')
<div class="container">
<div class="col-md-6" style="float:left;">
<h2>Todo List</h2>
<div class="table-hover">
<table class="table">
<thead class="thead-default">
<tr>
<th>Title</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(count($todos) > 0)
#foreach($todos as $todo)
<tr>
<th>{{$todo->title}}
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModal3Label{{ $loop->iteration }}">View task</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h3>{{$todo->title}}</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</th>
<td>{{$todo->status}}</td>
<td><a class="btn btn-sample btn-sm">Edit</a> <a class="btn btn-sample2 btn-sm">Delete<a></td>
</tr>
</tbody>
#endforeach
#else
<th><h2>Nothing to show</h2></th>
#endif
</table>
</div>
</div>

How to retrieve file id from table and pass it to modal in Laravel 5?

In my project I have a table which lists all the files uploaded by user with options to view or delete them. When the user clicks delete it opens a modal which asks for confirmation. Everything works fine except that when I click delete in any other file except the first file the modal always asks for confirmation to delete the first file. In short only the first file in the table gets deleted. I cannot retrieve the ID of other files, to pass it to deleteentry/{id}.
Here is the code with modal (myModal):
<tbody class="searchable">
#foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td>View</td>
<td>
Delete
<!-- Modal HTML -->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" style="color:IndianRed;">Confirmation</h4>
</div>
<hr>
<div class="modal-body" style="margin-top:-20px;">
<p>Do you want to delete the file {{ $entry->original_filename }} </a>permanently?</p>
<p class="text-primary" style="margin-top:-40px;"><small>If you click yes, the file will be deleted from our database permanently.</small></p>
</div>
<div class="modal-footer" style="margin-top:-40px;">
<button type="button" class="btn btn-flat btn-primary" data-dismiss="modal">No</button>
Yes
</div>
</div>
</div>
</div>
</td>
</tr>
#endforeach
</tbody>
It works when I remove the modal altogether and have just:
<tbody class="searchable">
#foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td>View</td>
<td>
Delete
</td>
</tr>
#endforeach
</tbody>
So it shows that the problem lies with the modal but I cant figure out the solution.
You're using the same value for the id attribute for all your confirmation modals, which is not allowed. So to avoid that you'll need to add some additional piece of data to the id attribute for each item. The easiest way to ensure they are unique is by appending $entry->id to each one like so:
<tbody class="searchable">
#foreach ($files as $entry)
<tr>
<td><b>{{ $entry->original_filename }}</b></td>
<td><b>{{ $entry->created_at }}</b></td>
<td>View</td>
<td>
Delete
<!-- Modal HTML -->
<div id="deleteModal_{{ $entry->id }}" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" style="color:IndianRed;">Confirmation</h4>
</div>
<hr>
<div class="modal-body" style="margin-top:-20px;">
<p>Do you want to delete the file {{ $entry->original_filename }} </a>permanently?</p>
<p class="text-primary" style="margin-top:-40px;"><small>If you click yes, the file will be deleted from our database permanently.</small></p>
</div>
<div class="modal-footer" style="margin-top:-40px;">
<button type="button" class="btn btn-flat btn-primary" data-dismiss="modal">No</button>
Yes
</div>
</div>
</div>
</div>
</td>
</tr>
#endforeach
</tbody>

Categories