normally i use this:
$users = \App\Users::where('age', '>=', '20')->paginate(20);
But if I use raw querys or use the get(); method from querybuilder, I got an array not a collection or a paginator instance as result.
How can I get my ->links(); from that array result?
Hope all is explained well. :-)
KR,
Marcel
Write{{ $users->links() }} in your view page. According to your query, pagination will show while your data return more than 20 rows.
For better understand Read this doc
Related
The pagination is like this:
Qitem::filter(Request::get("search") ?: "")
->withUserNames()
->withTrashed()
->paginate(10)
->onEachSide(1)
->withQueryString()
The returned data is different what is expected (even according to the docs):
According to docs, there should be other attributes like prev_page_url, next_page_url, and instead of items, there should be a data attribute, containing the data from the table. It even behaves so if I simplify the query like:
Qitem::paginate(10)
Has anybody experienced similar phenomena? Any suggestions? Thanks in advance.
For the purposes which response do you need, you can do it like:
JSON response
Create resource:
php artisan make:resource QItemResource
More about resources in the docs
Next in your controller:
$items = Qitem::filter(Request::get("search") ?: "")
->withUserNames()
->withTrashed()
->paginate(10)
->onEachSide(1)
->withQueryString();
return QItemResourec::collection($items);
And you will get next_page_url, prev_page_url & data.
Collection
Just simply collect paginated data with collect().
$items = Qitem::filter(Request::get("search") ?: "")
->withUserNames()
->withTrashed()
->paginate(10)
->onEachSide(1)
->withQueryString();
dd(collect($items));
I followed the Laravel 5.4 from Scratch series and am currently trying to implement pagination into the blog example.
It's easy to get pagination for the homepage:
Post::latest()->filter(request(['month', 'year']))->simplePaginate(5);
Problem: paginate() or simplePaginate() cannot be called like this:
public function index(Tag $tag){
$posts = $tag->posts->paginate(5);
return view('posts.index', compact('posts'));
}
So when I visit /posts/tags/{tag}, it tells me method paginate() doesn't exist. How am I supposed to paginate the results in that case, so that I can still hand over the Eloquent model of Post (which I need to display all the details)?
Thank you very much!
I'm grabbing some data in Laravel using the find method and only grabbing the id's I want:
$my_ids = array(1,4,5,10);
$results = Model::find($my_ids);
However, if I try to paginate via Model::find($my_ids)->paginate(10), it throws the error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate().
How can I query to only get specific model Id's back from the database while also using pagination?
Turns out I can use this syntax:
$results = Model::whereIn('id', $my_ids)->paginate(10);
I suggest use the eloquent query builder:
$results = Model::whereIn('id', $my_ids)->paginate(10);
I am new to laravel and I am trying to get a pagination function into my result pages, so I have the following function to generate results from query and I would like to have a pagination on the results page, but I don't seem to get it work correctly
public function showResults()
{
$selectedquery = Input::get('Annonces');
$what = Input::get('what');
$where = Input::get('where');
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->get();
return View::make('results',array('results' => $results));
}
Any Help?
Well, for one, you're missing the call to ->paginate(n). Right now, your closure is ->get(), which returns all results for your annonces table. This is good, but doesn't work for pagination. Change the function like so:
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->paginate(10);
This will return all results grouped into 10 results per page. Feel free to change that as you see fit.
Lastly, somewhere on your view where you display the results, you will need to use this code to display a page-viewer:
<?php echo $results->links(); ?>
<!-- OR -->
{{ $results->links(); }}
Also, be sure to check out the docs on Laravel's pagination. You'll find it's pretty comprehensive!
Laravel Pagination
Hope that helps!
I get results with join query but in view when I want to show it, it says: trying to get property of non object. I am working over a simple blogging system and I am trying to get blogs from following friends. Here is my view:
#foreach($blogposts as $posts)
<h4>{{ $posts->title }}</h4>
#endforeach
I am sure the problem is at view but I added my controller too, maybe needed and here is my controller:
$posts=DB::table('following')
->join('page_posts', 'following.p_id', '=', 'page_posts.p_id')
->select('page_posts.*')
->where('u_id','=',Auth::User()->id)
->orderBy('updated_at','desc')
->get();
return View::make('index')->with('blogposts',$posts);
Help me with this please.
Try this. You need to pass the values as like this.
return View::make('index',array('blogposts'=>$posts));
I should have used eloquent to solve this means that my controller should be like this:
$posts=following::join('page_posts', 'following.p_id', '=', 'page_posts.p_id')
->select('page_posts.*')
->where('u_id','=',Auth::User()->id)
->orderBy('updated_at','desc')
->get();
return View::make('index')->with('blogposts',$posts);
The problem solved. I posted the answer, because someone else maybe needs.