Return Action Button Shaped Text Datatable Laravel - laravel

I want to add action button to my datatable here the code
html table and datatables
<table id="users-table" class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>alamat</th>
<th>nohp</th>
<th>action</th>
</tr>
</thead>
</table>
<script id="script">
$(function () {
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: 'test/json'
});
});
</script>
heres the laravel json datatables
public function data(Datatables $datatables)
{
$builder = Kontak::query()->select('id', 'nama', 'email', 'alamat', 'nohp');
return $datatables->eloquent($builder)
->addColumn('action', 'users.datatables.intro')
->rawColumns(['action'])
->make();
}
but it keeps show result like this
result images

You should try ->escapeColumns([]) before ->addColumn('action', 'users.datatables.intro')

You can use the routes methods for your model.
view :
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>alamat</th>
<th>nohp</th>
<th>action</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->alamat }}</td>
<td>{{ $user->nohp }}</td>
<td>
<form action="{{ route('users.destroy' , ['id' => $user->id]) }}" method="post">
{{ method_field('delete') }}
{{ csrf_field() }}
<div class="btn-group btn-group-xs">
edit
<button type="submit" id="deleteButton" data-name="{{ $user->id }}" class="btn btn-xs btn-warning">delete</button>
</div>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
controller:
public function index()
{
$users = User::latest()->paginate(25);
return view('users.all' , compact('users'));
}
change users.all to your users view.

Related

How to fill the second html table by clicking a button from the first table when both of them are on the same page in Laravel?

I am using Laravel with mySql. I have two table in blade view, one in main div which contains a button to open the second which is in aside div and fill it with data retrieved from the controller. Please find below my code:
my routes
// showing list of orders
Route::get('orders/show/all', 'OrderController#showOrders')->name('orders.show.all');
// showing order details
Route::get('orders/details/{id}', 'OrderController#showOrderDetails')->name('orders.details');
my controller (OrderController), copied only two functions which have to do with this
public function showOrders(){
$orders = Order::orderBy('total_amount', 'desc')
->where('user_id','=', Auth::user()->id)
->get();
return view('orders.undo_sales', compact('orders'));
}
public function showOrderDetails($id){
$ordered_item = Order::findOrFail($id);
return redirect('orders/show/all', compact('ordered_item'));
}
}
my blade.php file
<main class="col-sm-8">
#include('partials.messages')
<div class="card">
<div class="card-header">List of all sales</div>
<div class="card-body">
<table class="table ">
<thead>
<tr>
<th>Order #</th>
<th>Order Date</th>
<th>Total Amount</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($orders as $order)
<tr>
<td>{{ $order->id }}</td>
<td>{{ $order->created_at }}</td>
<td>{{ number_format($order->total_amount) }}</td>
<td>
Order details
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</main>
<aside class="col-sm-4" id="aside">
<div class="card">
<div class="card-header">Order Details</div>
<div class="card-body">
<table class="table ">
<thead>
<tr>
<th>Item Name</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
#foreach ($ordered_item->items as $value)
<tr>
<td>{{ $value->item_name }}</td>
<td>{{ number_format($value->pivot->quantity_on_order) }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</aside>
</div>
<script>
let showDetails = document.querySelector('.show-details')
let aside = document.querySelector('#aside')
aside.style.display = 'none'
function showDiv(){
aside.style.display = 'block'
}
</script>
There is no problem with filling the main (first) table with data from the controller. The problem is getting the object returned by the function showOrderDetails($id) to the same view but on a different table and display those records. I tried to dd() the data in the controller on this method and I could see the item_name and the respective quantity. Which proved that there is no problem with this code in blade file:
#foreach ($ordered_item->items as $value)
<tr>
<td>{{ $value->item_name }}</td>
<td>{{ number_format($value->pivot->quantity_on_order) }}</td>
</tr>
#endforeach
But when I sent to the view I got the error in my view file:
Undefined variable: ordered_item
Please help me to get the data across.
Thank you.
What you're trying to do is to redirect to the same page with an additionnal variable. But it can't work like that.
A simple answer would be this:
web.php
Route::get('orders/show/all', 'OrderController#showOrders')->name('orders.show.all');
Controller.php
public function showOrders(){
$orders = Order::orderBy('total_amount', 'desc')
->where('user_id','=', Auth::user()->id)
->get();
// I have added the same condition. I believe, you don't want the user to be able to see all user's orders
$ordered_item = Order::where('user_id', '=', Auth::user()->id)
->find(request('order_id'));
return view('orders.undo_sales', compact('orders', 'ordered_item'));
}
}
blade.php
....
Order details
....
#if($ordered_item)
<aside class="col-sm-4" id="aside">
#foreach ($ordered_item->items as $value)
....
#endforeach
</aside>
#endif

Laravel - syntax error, unexpected ')', expecting '['

I am using Laravel 5.8 to create an App. When I wanted to display the view, it generated an error as shown below:
syntax error, unexpected ')', expecting '['
Controller
public function serviceOverview(Request $request)
{
$data['title'] = 'Subscription Overview';
$serviceoverviews = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
$render=[];
if(isset($request->package_name))
{
$serviceoverviews=$serviceoverviews->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
$render['package_name']=$request->package_name;
}
if(isset($request->service_id))
{
$serviceoverviews=$serviceoverviews->where('service_id',$request->service_id);
$render['service_id']=$request->service_id;
}
$serviceoverviews= $serviceoverviews->orderBy('created_at','DESC');
$serviceoverviews= $serviceoverviews->paginate(15);
$serviceoverviews= $serviceoverviews->appends($render);
$data['serviceoverviews'] = $serviceoverviews;
return view('report.serviceOverview',$data);
}
In the Controller, I tried to do some filtering. Also, I did raw query.
View
<div class="box box-primary">
<div class="box-header with-border">
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
#if(count($detailsubscriptions))
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->links() }}
</td>
</tr>
</tbody>
</table>
#else
<div class="row text-center">
<h2>No Service Overview to show</h2>
</div>
#endif
</div>
</div>
I tried to check the code, but found nothing. How do I resolve this issue
You have missed a $ from the $serviceoverview variable in your blade #foreach. Change:
#foreach($serviceoverviews as $key => serviceoverview)
To
#foreach($serviceoverviews as $key => $serviceoverview)
Here is the full code updated with changes. Please refer to this
Controller.php
public function serviceOverview(Request $request)
{
$builder = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
if($request->filled('package_name'))) {
$builder = $builder->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
}
if(isset($request->service_id))
{
$builder=$builder->where('service_id',$request->service_id);
}
$serviceoverviews = $builder->orderBy('created_at','DESC')->paginate(15);
$data = [
'title' => 'Subscription Overview',
'serviceoverviews' => $serviceoverviews
];
return view('report.serviceOverview', $data);
}
Now change your view code like this.
view.blade.php
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => $serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->appends(\Request::all())->links() }}
</td>
</tr>
</tbody>
</table>
You are using serviceoverview in place of $serviceoverview in view file. That would be an issue.
You have made some typo and one logical error.
Change
#foreach($serviceoverviews as $key => serviceoverview) to #foreach($serviceoverviews as $key => $serviceoverview)
<td>{{ serviceoverview->service_name }}</td> to <td>{{ $serviceoverview->service_name }}</td>
You have put {{ $serviceoverview->links() }} out side of the foreach loop

Passing data from database to view correctly

I am creating a simple crud application with file upload. But i have problems in displaying the thumbnail in view(index.blade.php). All images in database are displayed in each book instead of displaying one image in one book. Here is my controller.
public function index()
{
$books=UploadedFile::all();
$files=Upload::all();
return view('books.index',compact('books','files'));
}
and my index.blade.php
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="bg-info">
<th>Id</th>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Thumbs</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<tbody>
#foreach ($books as $book )
<tr>
<td>{{ $book->id }}</td>
<td>{{ $book->isbn }}</td>
<td>{{ $book->title }}</td>
<td>{{ $book->author }}</td>
<td>{{ $book->publisher }}</td>
<td>
#foreach ($files as $file )
<img src="{{asset('uploads/'.$file->filename)}}" height="40" width="50">
#endforeach
</td>
<td>Details</td>
<td>Update</td>
<td>
{!! Form::open(['method' => 'DELETE', 'route'=>['books.destroy', $book->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
I separated the table for the filename from the rest of the fields. and here is the result--

Loading data when button is clicked using vue js and laravel

I have this project that when a user clicks on a button the data should be displayed on a table. However, I am using a table as seen here http://i.stack.imgur.com/HW4rE.jpg. What I want is that when a user clicks on the add button on the enrollment form table, it should displayed on the added subjects table without the need of refreshing the page. The problem is that I am using a table and I cannot make use of the v-model to bind the values.
So here's my form.blade.php
Enrollment Form table
<table class="table table-bordered">
<tr>
<th>Section</th>
<th>Subject Code</th>
<th>Descriptive Title</th>
<th>Schedule</th>
<th>No. of Units</th>
<th>Room</th>
<th>Action</th>
</tr>
<body>
<input type="hidden" name="student_id" value="{{ $student_id }}">
#foreach($subjects as $subject)
#foreach($subject->sections as $section)
<tr>
<td>{{ $section->section_code }}</td>
<td>{{ $subject->subject_code }}</td>
<td>{{ $subject->subject_description }}</td>
<td>{{ $section->pivot->schedule }}</td>
<td>{{ $subject->units }}</td>
<td>{{ $section->pivot->room_no }}</td>
<td>
<button
v-on:click="addSubject( {{ $section->pivot->id}}, {{ $student_id}} )"
class="btn btn-xs btn-primary">Add
</button>
<button class="btn btn-xs btn-info">Edit</button>
</td>
</tr>
#endforeach
#endforeach
</body>
</table>
AddedSubjects Table
<table class="table table-bordered">
<tr>
<th>Section Code</th>
<th>Subject Code</th>
<th>Descriptive Title</th>
<th>Schedule</th>
<th>No. of Units</th>
<th>Room</th>
<th>Action</th>
</tr>
<body>
<tr v-for="reservation in reservations">
<td>#{{ reservation.section.section_code }}</td>
<td>#{{ reservation.subject.subject_code }}</td>
<td>#{{ reservation.subject.subject_description }}</td>
<td>#{{ reservation.schedule }}</td>
<td>#{{ reservation.subject.units }}</td>
<td>#{{ reservation.room_no }}</td>
<td>
<button class="btn btn-xs btn-danger">Delete</button>
</td>
</tr>
</body>
</table>
And here's my all.js
new Vue({
el: '#app-layout',
data: {
reservations: []
},
ready: function(){
this.fetchSubjects();
},
methods:{
fetchSubjects: function(){
this.$http({
url: 'http://localhost:8000/reservation',
method: 'GET'
}).then(function (reservations){
this.$set('reservations', reservations.data);
console.log('success');
}, function (response){
console.log('failed');
});
},
addSubject: function(id,student_id){
this.$http({
url: 'http://localhost:8000/reservation',
data: { sectionSubjectId: id, studentId: student_id },
method: 'POST'
}).then(function(response) {
console.log(response);
},function (response){
console.log('failed');
});
}
}
});
Can anyone suggets me on how to solve this problem, without the need of refreshing the page.

Laravel 5 unable to render pagination

I'm new to Laravel 5 and I have an issue displaying pagination. I looked in the documentation and just can't get it to work.
usercontroller:
public function getIndex()
{
$users = User::where('active', '=','verified')->simplePaginate(10);
return View::make('User.index')->with('users',$users);
}
index.blade.php:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>email</th>
<th>role</th>
<th>created</th>
<th class="actions">Actions</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->role }}</td>
<td>{{ $user->created_at }}</td>
<td class="actions">
blabla
</td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->render() !!}
for some reason, it was printing an additional /
fix:
{!! str_replace('users/','users',$users->render()) !!}

Resources