For some reason {{ $items->links() }} doesn't do anything.
Direct cause of this problem is that $items->getLastPage() returns 0. Same $items->getTotal().
I can change page parameter in the url and it works fine - it goes to correct page.
however $items->getCurrentPage() returns 1 on each page.
Pagination works perfectly for me with other models just this one gives me problems.
Also it seems to be working fine when I create the paginator manually but I want to be able access some methods from the model so I want to use Eloquent models and not raw array.
Edited:
Code:
$records = EventLog::byObject($model, $id)->paginate(10);
static public function byObject($model, $id)
{
$records = DB::select([query], [params]);
return self::getHistory($records, $model);
}
static public function getHistory($records, $model = '')
{
$ids = array();
foreach ($records as $record) {
array_push($ids, (int)$record->id);
}
$history = array();
if (count($ids)) {
$history = EventLog::whereRaw('id IN (' . implode(',', array_fill(0, count($ids), '?')) . ')', $ids)
->with('creator')
->with(array('eventfields' => function($query)
{
$query->whereRaw('field NOT LIKE \'%_id\'');
}))
->orderBy('event_logs.created_at')
->orderByRaw('(CASE WHEN eventable_type=? THEN 0 ELSE 1 END)', array($model))
->orderByRaw('(CASE WHEN parent_type=? THEN 0 ELSE 1 END)', array($model));
}
return $history;
}
The returned $history has the right type, is displayed correctly but the pagination is not working for some reason.
I am trying to display links using
{{ $records->links(); }}
Thanks
I've same problem here, laravel 4.1 and a query scope with fulltext search. In my case the problem was the orderByRaw (a normal orderby doesn't broke pagination links).
So my "poor" solution is to keep orderByRaw off when I need a pagination and use:
->orderBy(DB::raw('my raw orderby'));
that works
Related
I have query like this.
$types = Types::where('id', '=', '2')->with('purposes', function ($query) {
$query->withCount('pool');
})->first();
This query works well and i have this foreach. This foreach gives exactly what i want do and it works in controller but not working in blade completely. It gives types_name but count is not working. However when it gives count in controller. What should i do?
Foreach($types as $item){
$item->pool_name;
$item->pool_purposes_count;
}
Your code should be:
$item = Types::->with('purposes', function ($query) {
$query->withCount('pool');
})->find(2);
Then you can use, in blade:
$item->pool_name;
$item->pool_purposes_count;
I've followed the instructions on the Laravel documentation for pagination with appends([]) however I'm having a little trouble with the persistence of these parameters.
Say for example, I pass home?category=Cars&make=Tesla to my view. What is the best way to paginate with them Get requests?
Right now I've passed the category as a parameter to the view as (where category is the model i've grabbed findOrFail with the request('category');)
$category_name = $category_model->name;
And then in my view it's like so:
{{ $vehicles->appends(['category' => $category_name])->links() }}
But when I go between pages in the pagination, this $category_name value doesn't seem to persist. Whats the recommended way to achieve what I want?
Thanks.
You can append the query string in your controller when you paginate the result. I'm not sure if that was your only question or even regarding applying the query string as a condition. So here is a sample showing you how to do both. This should give you an idea of how to do it. I just assumed the column names in this example.
$category = request('category');
$make = request('make');
$vehicles = Vehicle::when($category, function ($query) use ($category) {
return $query->where('category', $category);
})
->when($make, function ($query) use ($make) {
return $query->where('make', $make);
})
->paginate(10);
$vehicles->appends(request()->query());
return view('someview', compact('vehicles'));
Having problems getting my pagination to work in Laravel 5.2 I use a foreach to generate a list of objects where each object has a certain ranking. (competition)
The first query I used was this one:
$goedeDoelen = GoedDoel::orderBy('punten', 'desc')->simplePaginate(5);
This worked pretty ok, only problem was that my ranking would reset everything I would go to a different page.
Example: Page 1 has objects from rank 1 - 5, page 2 should have ranks 6-10. By using the first Paginate method, the second page would have objects starting from 1 again.
I have tried to work around this by adding the ranking as an extra attribute to my Eloquent collections.
$ranking = GoedDoel::orderBy('punten', 'desc')->get();
foreach($ranking as $key => $item) {
$item->ranking = $key+1;
}
After that I tried to use ->simplePaginate() on my updated collection. This gave an error.
I have created a custom Paginator.
$goedeDoelen = new Paginator($ranking, 5);
This isn't working as intended. When I go to my second page, the URL messes up and goes to another view.
How can I make sure the Paginator knows what my current URL is to which it has to apply the ?page=2
You need to use the paginate() method.
$goedeDoelen = GoedDoel::orderBy('punten', 'desc')->paginate(5);
{!! $goedeDoelen->links() !!}
The following Code illustrates manual pagination in Laravel
Sample Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use App\Models\UserRechargeDetails;
class PaginateController extends Controller
{
//
public function index(Request $request)
{
$user_1 = new UserRechargeDetails;
// Get records from Database
$items = $user_1->all();
// Store records in an array
$records = [];
$i = 0;
foreach($items as $item)
{
$records[$i][0] = $item->user_name;
$records[$i][1] = $item->rech_mobile;
$i++;
}
// Current page for pagination
$page = $request->page;
// Manually slice array of product to display on page
$perPage = 2;
$offset = ($page-1) * $perPage;
$data = array_slice($records, $offset, $perPage);
// Your pagination
$final_data = new Paginator($data, count($records), $perPage, $page, ['path' => $request->url(),'query' => $request->query(),]);
/*
For Display links, you may add it in view page
{{ $data->links('pagination::bootstrap-4') }}
*/
return view('admin.pagination_new', ['data' => $final_data, 'j' => 1]);
}
}
hi :) im parsing my database in a table with pagination !!
this is my controller
$theme = Theme::all();
$questions = questionList::paginate(10);
$the = "";
return View::make('home.home')
->with('user',Auth::user())
->with('theme', $theme)
->with('the' , $the)
->with('questions',$questions);
and my view i have {{ $questions->links(); }} under my table and it's working fine !!
the thing is that i have a list of themes to sort data in the table so when i click on divertisement i get divertisement data.
the problem is that when i paginate it return to the get request and give me all the data !! what is the problem thx :)
To add a filter/sort you also need to add that in a where clause in your query and also you need to append the query string in your pagination links. For example:
public function showPosts()
{
$questions = app('questionList');
if($filter = Input::get('filter')) {
$questions = $questions->where('theme', $filter);
}
$questions = $questions->paginate(10);
if(isset($filter)) {
$questions->appends('filter', $filter);
}
return View::make(...)->with(...);
}
In your view you need to create links to this method (Probably using a route name or url) with filter query string. So, for example:
Divertisement
SomethingElse
I wrote in my controller a conditional filter working like this:
$this->_view = View::factory('crud/index')
->bind('collection', $collection);
$collection = ORM::factory($this->_model);
if (Request::current()->method() === Request::POST)
{
foreach (Request::current()->post('filter') as $field => $value)
{
$collection->where($field, '=', $value);
}
}
$collection->find_all();
And in the view I have a conditional to display a message if there are no filtered results or rows in database.
<?php if ( ! $collection->count()): ?>
This gives me an exception:
Kohana_Exception [ 0 ]: Invalid method count called in Model_Product
The problem is that before adding the filter, my controller action was:
$this->_view = View::factory('crud/index')
->bind('collection', $collection);
$collection = ORM::factory($this->_model)->find_all();
And $collection->count() worked just fine in the view. Why is the ORM find_all() method returning a model even if I don't post, even if the code is not entering the conditional? Just breaking $collection = ORM::factory($this->_model)->find_all(); into $collection = ORM::factory($this->_model); and $collection->find_all(); breaks the whole thing. Why is that strange behavior?
Thanks.
Try doing this:
$collection = $collection->find_all();
find_all() doesn't save the query results in the ORM object, you need to save it in a variable.