How to do paginate in Laravel? - php

so what i want is to use paginate in my view but i got always an error Call to a member function paginate() on a non-object, this is my code :
First my Action :
public function index()
{
$logs = DB::table('services')
->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})
->get()->paginate(10); //here i got the error
return View::make('logs.index',array('logs'=> $logs,'title'=>'Service Logs'));
}
Then my view :
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Domain</td>
<td>Port</td>
<td>Checktime</td>
<td>Status</td>
<td>Responce</td>
</tr>
</thead>
<div class="container">
#foreach($logs as $key => $value)
<tr>
<td>{{ $value->domain }}</td>
<td>{{ $value->service_port }}</td>
<td>{{ $value->checktime }}</td>
<td class="text-center">
#if( $value->status == 'up' ) <img src="../img/up3.png" />
#elseif( $value->status == 'down' ) <img src="../img/down3.png" />
#else <img width="30" height="30" src="../img/warning_icon.png" />
#endif
</td>
<td>{{ $value->response_time }}</td>
</tr>
#endforeach
</div>
</table>
{{$logs->links();}}
So please if someone has any idea i will be so appreciative

You do not need to use get when you are using pagination. Your query should simply be:
$logs = DB::table('services')->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})->paginate(10);
In fact you can remove that closure as well since this really isn't a complex join.
$logs = DB::table('services')
->join('logs', 'services.id', '=', 'logs.service_id')
->paginate(10);

From the docs (http://laravel.com/docs/pagination) it seems that the correct usage is:
$logs = DB::table('services')
->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})
->paginate(10); //removed get()

Related

Laravel Blade not showing data from controller

I want to show data in tabular form in Laravel Blade View. Now, data is not showing up in blade, but when I check in controller using print_r it shows up Following is the code for Controller:-
public function index(){
$pickup = PickupLocation::select('*')
->whereNull('deleted_at')
->get()
->toArray();
$page = 'pickup_locations';
return view('backEnd.pickupManagement.index',compact('page'));
}
Following is the code in web.php:-
Route::match(['get','post'],'pickup-locations','backEnd\pickupManagement\PickupController#index');
And following is the code I am using View:-
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Address</th>
<th width="13%">Action</th>
</tr>
</thead>
<tbody>
#if(!empty($pickup))
#foreach($pickup as $key=>$value)
<tr>
<td>{{ ucfirst($value['name']) }}</td>
<td>{{ ucfirst($value['contact_no']) }}</td>
<td>{{ $value['location'] }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
add $pickup variable as second argument in your compact method.
Try like this
return view('backEnd.pickupManagement.index',compact('page', 'pickup')); }
Happy codding

Trying to get property 'expiry_date' of non-object in laravel

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()){

PHP Laravel - Nested blade loop requiring SQL query

I am trying to return a loop to my blade template file with below result:
Main Name
Sub name #1
Sub name #2
Main Name
Sub name #1
Sub name #2
etc..
I have created a controller for this:
MergerController.php
public function index()
{
$MainOrganisations = DB::table('test')
->select('address', 'code', DB::raw('count(*) as amount'))
->where('code', 'LIKE', '64%')
->groupBy('address')
->orderBy('amount', 'desc')
->get();
foreach($MainOrganisations as $value){
$SubOrganisations = DB::table('test')
->select('name', 'code')
->where('address', $value->address)
->where('code', 'NOT LIKE', '64%')
->get();
}
$data = [
'MainOrganisations' => $MainOrganisations,
'SubOrganisations' => $SubOrganisations,
];
#Return data to our view
return view('merger.list')->with($data);
}
Then, inside my view file, I try to show this data:
list.blade.php
<table>
#foreach ($MainOrganisations as $Main)
<thead>
<th colspan="2">{{ $Main->address }}</th>
</thead>
<thead>
<th>Name</th>
<th>Code</th>
</thead>
<tbody>
#foreach($SubOrganisations as $Sub)
<tr>
<td>{{ $Sub->name }}</td>
</tr>
#endforeach
</tbody>
#endforeach
</table>
However, above only return the first result from $SubOrganisations, and then loops ONLY that result in my nested foreach:
#foreach($SubOrganisations as $Sub)
<tr>
<td>{{ $Sub->name }}</td>
</tr>
#endforeach
Returns:
Sub name #1
Sub name #1
Sub name #1
The way you are looping through each data for sub organization is wrong. Its better yo use eloquent relationship with eager loading for better performance.
Change your controller code:
public function index()
{
$MainOrganisations = DB::table('test')
->select('address', 'code', DB::raw('count(*) as amount'))
->where('code', 'LIKE', '64%')
->groupBy('address')
->orderBy('amount', 'desc')
->get();
foreach($MainOrganisations as $key => $MainOrganisation){
$SubOrganisations = DB::table('test')
->select('name', 'code')
->where('address', $MainOrganisation->address)
->where('code', 'NOT LIKE', '64%')
->get();
$MainOrganisation->subOrganisations = $SubOrganisations;
}
$data = [
'MainOrganisations' => $MainOrganisations
];
#Return data to our view
return view('merger.list')->with($data);
}
In you view:
<table>
#foreach ($MainOrganisations as $Main)
<thead>
<th colspan="2">{{ $Main->address }}</th>
</thead>
<thead>
<th>Name</th>
<th>Code</th>
</thead>
<tbody>
#foreach($Main->subOrganisations as $Sub)
<tr>
<td>{{ $Sub->name }}</td>
</tr>
#endforeach
</tbody>
#endforeach
</table>

How to display count result to blade template

I have attendance table then I perform count to total all the Present(P)Absent(A)Late(L) now how can I show it to blade template inside the html input with 3 separated value example: value=P, value=A, value=L;
right now with my code I'm getting it as all
controller:
public function get_status(){
$from = date('2018-01-01');
$to = date('2018-03-31');
$atnds = DB::table('attendances')
->select(DB::raw('count(*) as total, status'))
->where('status', '<>', 'status')
->whereBetween('days', [$from,$to])
->groupBy('status')
->where('lead_id', '=', 1)
->get();
return view('total',compact('atnds'));}
DD RESULT
[{"total":7,"status":"A"},{"total":9,"status":"L"},{"total":65,"status":"P"}]
If you want with input then you can use it like this
<table style="width:100%">
<tr><th>Status </th> <th>Total</th></tr>
#foreach($atnds as $at)
<tr>
<td>{{ $at->status }}</td>
<td><input type="text" value ="{{ $at->total }}" /></td>
</tr>
#endforeach
</table>
In blade you could use
<table style="width:100%">
#foreach($atnds as $ad)
<tr>
<td>{{ $ad->total }}</td>
<td>{{ $ad->status }}</td>
</tr>
#endforeach
</table>
If you want to break them out i would do so before returning it to the blade
$status = []
foreach($atnds as $ad) {
$status[$ad->status] => $ad->total
}
$status = collect($status);
return view('total',compact('status'));
and in the blade you should now be able to do
{{ $status->L }}
or
{{ $status['L'] }}

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.

Categories