pagination in laravel 5.3 - php

I want to add pagination to sample table 'passenger'.I have downloaded pagination package by php artisan vendor:publish --tag=laravel-pagination .In my controller code I have added paginate function.table displays one row but pagination controller is not shown (<<1 2 3>>).I want to show pagination control. How can I get pagination control.
Contrller travelApprovalController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class travelApprovalController extends Controller {
public function index(){
$users = DB::table('passenger')->paginate(1);
return view('approval_view',['users'=>$users]);
}
}
approval_view.vlade.php
<table border = 1>
<tr>
<td>ID</td>
<td>Passanger Name</td>
<td>Destination</td>
<td>Created Date</td>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->p_id }}</td>
<td>{{ $user->p_name }}</td>
<td>{{ $user->destination }}</td>
<td>{{ $user->created_date }}</td>
</tr>
#endforeach
</table>
web.php (route)
Route::get('view-records','travelApprovalController#index');

<table border = 1>
<tr>
<td>ID</td>
<td>Passanger Name</td>
<td>Destination</td>
<td>Created Date</td>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->p_id }}</td>
<td>{{ $user->p_name }}</td>
<td>{{ $user->destination }}</td>
<td>{{ $user->created_date }}</td>
</tr>
#endforeach
{{$users->links()}}
</table>

Related

Using DataTables in Laravel without aJax - No data available in table

I have a regular Laravel view that renders a table. When I try to use Datatables on it, it does render the table but it seems to want to format it before the table is actually rendered. I'm confused because, by the time the view is rendered, the server-side work is already done. The only thing it's doing is to render the table with the object passed from the controller.
<table id="stockmovement" class="table table-hover" style="width:100%">
<tbody>
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
<th colspan="3"></th>
</thead>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>
$(document).ready(function() {
$('#stockmovement').DataTable();
})
This is what I end up with:
I don't want to use AJAX to form my table. I've used plenty of vanilla PHP and DataTables instances before where it works just fine. I would appreciate any pointers on what I may be missing.
You have placed <tbody> in the wrong place. <tbody> should use under the </thead>.
Here is the code example:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>
Add tr tag inside the thead tag.
The foreach loop should be in the opening and closing tbody tag.
It should look like this:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<tr>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</tr>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>

How to use pluck to get specific data from other table in Laravel?

From SalesController, I want to get specific name from table Item and passed to HTML.
My controller looks like
public function index()
{
$items=Item::orderBy('name','ASC')->get()->pluck('name','id');
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
My HTML is
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Detail</th>
</tr>
</thead>
<tbody>
#foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->items->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
#endforeach
</tbody>
But I get the following error after trying to access the page:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\inventoryApp\resources\views\Sale\index.blade.php)
there are two way
1.Using relationship
class Sale extends Model
{
public function item()
{
return $this->belongsTo('App\Item','item_id','id');
}
}
so you can use like below
<td>{{ $sale->item->name }}</td>
2. Using array data
public function index()
{
$items=Item::orderBy('name','ASC')->pluck('name','id')->toArray();
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
<td>{{ $items[$sale->item_id] }}</td>
$items=Item::orderBy('name','ASC')->pluck('name','id');
and use $items directly without sale object
If you have relationship with table Sale.
Add this function to your Sale Class.
public function item()
{
return $this->belongsTo('App\Item');
}
In your controller you can use compact
public function index()
{
$items=Item::orderBy('name','ASC')->get();
$sales=Sale::all();
return view('Sale.index')->compact('sales','items');
}
And you can use Eager load to your HTML.
<tbody>
#foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->item->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
#endforeach
</tbody>
You are getting this error because you are trying to get a property of item name that does not exist on the sale object. you can simply do it by eloquent relationship as follows:
On your sale model:
public function item()
{
return $this->belongsTo('App\Item','item_id');
}
On your controller:
public function index()
{
$sales=Sale::all();
return view('Sale.index',compact('sales'));
}
Then on your blade you can easily get related Item name:
#foreach($sales as $sale)
<tr>
<td>{{ $sale->item->name }}</td>
</tr>
#endforeach

Laravel paginate on query builder

If i use
$categories= Category::orderby('id', 'desc')->paginate(3);
it works but if i use
$categories= Category::with('subcategories')->orderby('id', 'desc')->paginate(3);
paginate doesn't work! how to fix it?
UPDATE:
<tbody>
#foreach ($categories as $category)
#foreach($category->subcategories as $sub)
<tr>
<td class="text-center">{{ $sub->id }}</td>
<td class="text-center">{{ $sub->title }}</td>
<td class="text-center">{{ $sub->category->title }}</td>
</tr>
#endforeach
#endforeach
</tbody>
You need to approach this problem from the opposite side of the relationship.
Find all subcategories, with their parent category, order them by ID descending and then paginate to three per page.
$subcategories = Subcategory::with('category')
->orderBy('id', 'desc')
->paginate(3);
<tbody>
#foreach($subcategories as $subcategory)
<tr>
<td class="text-center">{{ $subcategory->id }}</td>
<td class="text-center">{{ $subcategory->title }}</td>
<td class="text-center">{{ $subcategory->category->title }}</td>
</tr>
#endforeach
</tbody>

Check if no records exist in view laravel

Okay so I've hit a wall for some reason and I can't figure this out. I wan't to check if there are any records in a query and then use an #if in my view to say either "yes, there are records" or "none". Nothing I've tried is working..
What I want:
<div class="col-md-12 col-style">
<h1 class="no-border">YOUR LATEST CONVERSIONS</h1>
#if(something here that lets me test if $transactions is empty)
<table class="table table-striped">
<tr>
<th>#</th>
<th>Type</th>
<th>User</th>
<th>Amount</th>
<th>Value</th>
<th>Result</th>
<th>Result Value</th>
<th>Date</th>
</tr>
#foreach($transactions as $transaction)
<tr>
<td>{{ $transaction->id }}</td>
<td>{{ $transaction->status }}</td>
<td>{{ $transaction->username }}</td>
<td>{{ $transaction->amount }}</td>
<td>{{ $transaction->value }}</td>
<td>{{ number_format($transaction->result, 2) }}</td>
<td>{{ $transaction->result_value }}</td>
<td>{{ $transaction->created_at }}</td>
</tr>
#endforeach
</table>
#else
yo no records
#endif
</div>
My controller:
public function index($username)
{
$user = User::where('username', $username)->firstOrFail();
$id = $user->id;
$transactions = Transactions::where('user_id', '=', $id)->take(5)->get();
return view('user.profile', compact('user', '=', 'userCurrency', 'transactions'));
So if you see in the top part there's a basic #if. Maybe I'm over thinking it, or am I passing in something that cant be checked the way I want it to?
Thanks!
#if (!$transactions->isEmpty())
...
#endif
this use laravel collection method do the trick.
it is as easy as:
#if(count($transactions) > 0)
#if($transactions) should do the trick.

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Categories