Datatable is not working when there is data from DB - php

Jquery datatable plugin is not working when it has data that selected from database by date. But it's working if there is no data in table. Here some code
Select all data from database table in controller :
$inbound['inbound'] = DB::table('REPORT_INBOUND')
->where('regdate', '=', date('Y-m-d', strtotime("2019-05-21")))
->get();
return view('/traffic', $inbound, $outbound);
And i did copy paste that necessary scripts for example :
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="ahhhaa" width="90%" class="table table-striped table-bordered table-hover display">
<thead class="thead-light">
<tr>
<th>ID</th>
<th>RegDate</th>
<th>HPMN Code</th>
<th>Country</th>
<th>HPMN Name</th>
</tr>
</head>
<tbody>
#foreach($inbound as $value)
<tr class="table table-hover">
<td>{{ $value->id }}</td>
<td>{{ $value->regdate }}</td>
<td>{{ $value->hpmn_code }}</td>
<td>{{ $value->country }}</td>
<td>{{ $value->hpmn_name }}</td>
#endforeach
</tbody>
</table>
$(document).ready(function () {
$('#ahhhaa').DataTable()
});
Is there any error ?

Maybe because u did not close your thead.
Fix this and comment if not solved .
<table id="ahhhaa" width="90%" class="table table-striped table-bordered table-hover
display">
<thead class="thead-light">
<tr>
<th>ID</th>
<th>RegDate</th>
<th>HPMN Code</th>
<th>Country</th>
<th>HPMN Name</th>
</tr>
</thead><!-- Add this Line -->
<tbody>
loop...
</tbody>
Hope this works !

return your view page with data:
return view('folder/file_name')->with($inbound);

Change the id of the table.Otherwise the laravel will not identify the table as a datatable.
<table id="datatable-buttons" class="table table-responsive">

Related

Displaying data from db in table form laravel blade

I tried to display all menus from the db in table form however i got an error saying that "undefined variable:menus(0)"
dashboard_index.blade.php
<div class= "menu-content">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Category Code</th>
<th scope="col">Menu Title</th>
<th scope="col">Menu Price</th>
</tr>
</thead>
<tbody>
#foreach ($menus as $menu)
<tr>
<td>{{ $menu->id }}</td>
<td>{{ $menu->category_code }}</td>
<td>{{ $menu->menu_title }}</td>
<td>RM{{ $menu->menu_price }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
web.php
Route::get('/dashboard', 'AdminMenuController#index');
AdminMenuController.php
public function index()
{
$menus = \App\Menu::all();
return view('admin.dashboard_index',
[
'menus'=>$menus,
]);
}
You have a syntax error in $menu = \App\Menu::all()
it should be $menus = \App\Menu::all()

Passing data to controller on row bases

I have a table display and each row contains a button. With this button l have to pass the id to the controller where using this Admin can view user details one by one.
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<button type="submit" class="btn btn-
default" name="viewcv"> View</button> <br>
<br>
</td>
</tr>
#endforeach
</tbody>
</table>
Please check the code , hope it helps:
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>CV</td>
</tr>
#endforeach
</tbody>
</table>
Assuming you want to pass the field "id" or change it according to your requirement.
If you want to link to a show view, try something like this:
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<a href="{{action('JobController#show', $row->id)}}" class="btn btn-default">View</button>
</td>
</tr>
#endforeach
This will create a link to a view, where you can show the data.
In your controller JobController, you must create a function that receives the data:
public function show(Request $request, $id) {
...
}
Just pass the id of the record to controller function and get the details from database.
<td>
View <br>
</td>

CSS not loading in Laravel's blade (5.3)

This is my blade view:
#extends('emails.newlayout')
#section('title', 'Reminder')
#section('content')
<?php $total = 0; ?>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach ($sallary as $emp)
<tr>
<td>{{ $emp->employee['name'] }}</td>
<td>{{ $emp['value'] }}</td>
<?php $total += $emp['value']; ?>
</tr>
#endforeach
</tbody>
</table>
<p>Total: US${{ $total }}</p>
#stop
And this is within the header of my newlayout.blade.php:
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
Works just fine on my browser when I open it as html, but when I render it on laravel it just doesn't load the bootstrap css at all (loads everything else). I wonder what am I doing wrong?
OUTPUT
<table class="m_-7473167048951624218table m_-7473167048951624218table-striped">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test 2</td>
<td>1,093.55</td>
</tr>
<tr>
<td>Test 3</td>
<td>1,090.33</td>
</tr>
</tbody>
</table>
Solved it
Embarrassing, but turns out it's because of e-mail. Every style should be inline css on an email, otherwise it doesn't render well according to a plethora of documentation about styling mails online.

pagination not working when clicking on page number

I am using laravel pagination. When I click on next page or any page number, nothing happens.
Controller function
public function getIndex()
{
$articles = Inspector::paginate(15);
return view('admin.inspector.test', compact('articles'));
}
View file
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}
Any help is much appreciated...Thanks...
public function getIndex()
{
return view('admin.inspector.test');
}
just return view while calling the action. dont send any data from the controller, just load the view,
when the view loads, fetch the db connection with pagination method. and render the pagination as below
<?php $articles = DB::connection('table_name')->where('if any condition')->get(); ?>
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$name

I am trying to get data from my table and show it in my view and paginate this data. I received this problem and couldn2t find any solution. I did the exactly same thing in my previous project and it worked well.
here is my Controller
public function hadiBirlikteCalisalimShow (){
$users =DB::table('birlikte_calisalim')->paginate(15);
return view('admin.birliktecalisalim',compact(['users']));
}
and this is my view
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Email</th>
<th>Tarih</th>
<th>İstek</th>
</tr>
</thead>
<tbody>
#foreach($users as $row)
<tr>
<td>{{$users->name}}</td>
<td>{{$users->email}}</td>
<td>{{$users->tarih}}</td>
<td>{{$users->message}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$users->links()}}
#foreach($users as $row)
<tr>
<td>{{$row->name}}</td>
<td>{{$row->email}}</td>
<td>{{$row->tarih}}</td>
<td>{{$row->message}}</td>
</tr>
#endforeach
should be $row->name,$row->email, etc... not $users->name, and change {{$users->links()}} to {!! $users->render() !!}

Categories