How can I put "paginate" method in my controller index function? I am going to put paginate method to view my data in my controller index function. I have been configured all things with paginate and but I am unable to guess where should I put following paginate in the index function,
paginate(10);
my index function,
public function index()
{
$vehicles = Vehicle::with('uploads')->get()->sort(function ($a, $b) {
if ($a->adtype !== $b->adtype) {
return $b->adtype - $a->adtype;
}
return $a->adtype
? ($a->updated_at->gt($b->updated_at) ? -1 : 1)
: ($a->created_at->gt($b->created_at) ? -1 : 1);
});
return view('vehicles.index')->withVehicles($vehicles);
}
where should I put them?
Try this one
$vehicles = Vehicle::with('uploads')->get()->sort(function ($a, $b) {
if ($a->adtype !== $b->adtype) {
return $b->adtype - $a->adtype;
}
return $a->adtype
? ($a->updated_at->gt($b->updated_at) ? -1 : 1)
: ($a->created_at->gt($b->created_at) ? -1 : 1);
})->paginate(10);
@Ishatanjeeb it is got following error,
BadMethodCallException
Method paginate does not exist.
The problem is that you are currently getting the whole dataset as a collection and then sorting it. This is very inefficient memory wise and will not scale.
You need to add the sorting as orderBy to the query and then paginate the results
I don't know because I cannot for the life of me workout what that sorting is doing
Try this one
$vehicles = Vehicle::with('uploads')->sort(function ($a, $b) {
if ($a->adtype !== $b->adtype) {
return $b->adtype - $a->adtype;
}
return $a->adtype
? ($a->updated_at->gt($b->updated_at) ? -1 : 1)
: ($a->created_at->gt($b->created_at) ? -1 : 1);
})->paginate(10);
Please explain in words, how you want the dataset ordered?
@Snapey please see My blade file,
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="col-md-3 ">
@include('_includes.nav.usermenu')
</div>
<div class="col-md-7 col-md-offset-1">
<form method="GET" action="{{ url('search') }}">
{{csrf_field()}}
<div class="row">
<div class="col-md-6">
<input type="text" name="search" class="form-control" placeholder="Search">
</div>
<div class="col-md-6">
<button class="btn btn-info">Search</button>
</div>
</div>
</form>
<br>
@forelse( $vehicles as $vehicule )
@if( $vehicule->uploads->count() > 0 )
<a href="{{ route('vehicles.show', $vehicule->id) }}">
@php
$upload = $vehicule->uploads->sortByDesc('id')->first();
@endphp
<div style="border-style: solid; color: {{ $vehicule->adtype === 1 ? 'black' : 'blue' }} ">
<img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
{{ Carbon\Carbon::parse($vehicule->created_at)->diffForHumans()}}
{{$vehicule->provincename}}
{{$vehicule->milage}}
</div>
<br>
<hr>
@endif
@empty
<td>No Advertisment to display.</td>
@endforelse
</div>
</div>
</div>
{{$vehicule->links()}} //paginate showing links
</div>
@endsection
I need show pagination links in this blade file
Please answer the question. I'm not interested in the blade - its not relevant to the question.
You can paginate the results like this;
public function index()
{
$vehicles = Vehicle::with('uploads')->paginate(10);
return view('vehicles.index')->withVehicles($vehicles);
}
But the results are not sorted. You need to explain what the sorting does if you want help in having pagination AND sorting
@Snapey I need show some shoring results according to My Business logic. that means I need show data according to adtype of vehicle columns if adtype 1 then those ads should show before the adtype values 0 . and adtype 1 should show according to table updated_ad and adtype values 0 should show according to created_at values of the table values.
I don't know the difference between the types, but sorting by updated_at and created_at (according to add type) is making it much harder.
In the case of adtype=0 are updated_at and created_at actually the same value?
@Snapey simply this is the logic I need. adtype maintain payment ad or free ad 1= payment ad and 0= free ad. and "adtype=1" records should show according to updated_at values. and free ads "adtype=0" should show according to created_at values of the table.
Thats what I am challenging.. Why cannot free ads also be ordered by updated_at?
public function index()
{
$vehicles = Vehicle::with('uploads')
->orderBy('adtype','DESC')
->latest()
->paginate(10);
return view('vehicles.index')->withVehicles($vehicles);
}
The query becomes a lot more complicated to use a different sort field depending on the adtype.
Try this one
$vehicles = Vehicle::with('uploads')->sort(function ($a, $b) {
if ($a->adtype !== $b->adtype) {
return $b->adtype - $a->adtype;
}
return $a->adtype
? ($a->updated_at->gt($b->updated_at) ? -1 : 1)
: ($a->created_at->gt($b->created_at) ? -1 : 1);
})->paginate(10);
@Snapey got this error,
ErrorException
Call to undefined method Illuminate\Database\Query\Builder::links() (View: C:\Users\Msoft\Desktop\ddddy\resources\views\vehicles\index.blade.php)
Show your controller code as it is now.
@Snapey My Controller,
public function index()
{
$vehicles = Vehicle::with('uploads')
->orderBy('adtype','DESC')
->latest()
->paginate(10);
return view('vehicles.index')->withVehicles($vehicles);
}
In the view
{{$vehicule->links()}} //paginate showing links
should be
{{ $vehicles->links() }}
@Snapey it is working but violate My business logic. that means current my application adtype=1 values show latest updated ads in the view file. but in your index function show only adtype=1 values in the top of the pades and not print latest updated_at records on the top
Do you have Laravel Debugbar installed to check the query?
->orderBy('adtype','DESC') should put the adtype =1 and then adtype=0
->latest() by default is using created_at which is wrong for adtype=1 so change this line to ->latest('updated_at)
@Snapey it is working fine now. Thanks
Please sign in or create an account to participate in this conversation.