Laravel Eloquent limit and offset - laravel

This is mine
$art = Article::where('id',$article)->firstOrFail();
$products = $art->products;
I just wanna take a limit 'product'
This is wrong way
$products = $art->products->offset($offset*$limit)->take($limit)->get();
Please give me a hand!
Thanks!

skip = OFFSET
$products = $art->products->skip(0)->take(10)->get(); //get first 10 rows
$products = $art->products->skip(10)->take(10)->get(); //get next 10 rows
From laravel doc 5.2 https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset
skip / take
To limit the number of results returned from the query, or to skip a
given number of results in the query (OFFSET), you may use the skip
and take methods:
$users = DB::table('users')->skip(10)->take(5)->get();
In laravel 5.3 you can write (https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset)
$products = $art->products->offset(0)->limit(10)->get();

Quick:
Laravel has a fast pagination method, paginate, which only needs to pass in the number of data displayed per page.
//use the paginate
Book::orderBy('updated_at', 'desc')->paginate(8);
how to customize paging:
you can use these method: offset,limit ,skip,take
offset,limit : where does the offset setting start, limiting the amount of data to be queried
skip,take: skip skips a few pieces of data and takes a lot of data
for example:
Model::offset(0)->limit(10)->get();
Model::skip(3)->take(3)->get();
//i use it in my project, work fine ~
class BookController extends Controller
{
public function getList(Request $request) {
$page = $request->has('page') ? $request->get('page') : 1;
$limit = $request->has('limit') ? $request->get('limit') : 10;
$books = Book::where('status', 0)->limit($limit)->offset(($page - 1) * $limit)->get()->toArray();
return $this->getResponse($books, count($books));
}
}

laravel have own function skip for offset and take for limit. just like below example of laravel query :-
Article::where([['user_id','=',auth()->user()->id]])
->where([['title','LIKE',"%".$text_val."%"]])
->orderBy('id','DESC')
->skip(0)
->take(2)
->get();

You can use skip and take functions as below:
$products = $art->products->skip($offset*$limit)->take($limit)->get();
// skip should be passed param as integer value to skip the records and starting index
// take gets an integer value to get the no. of records after starting index defined by skip
EDIT
Sorry. I was misunderstood with your question. If you want something like pagination the forPage method will work for you. forPage method works for collections.
REf : https://laravel.com/docs/5.1/collections#method-forpage
e.g
$products = $art->products->forPage($page,$limit);

Maybe this
$products = $art->products->take($limit)->skip($offset)->get();

$products = Article::orderBy('id', 'asc')->limit($limit)->get();

Try this sample code:
$art = Article::where('id',$article)->firstOrFail();
$products = $art->products->take($limit);

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);
$chunk = $collection->forPage(2, 3);
$chunk->all();

Please try like this,
return Article::where('id',$article)->with(['products'=>function($products, $offset, $limit) {
return $products->offset($offset*$limit)->limit($limit);
}])

Laravel 5.1< (current v 9)
$art->products->skip($offset)->take($limit)->all();
https://laravel.com/docs/9.x/collections#method-take

Related

How I can paginate DB::select(...) result and get links() method?

I have a big and difficult SQL query. It works fine for me. And I have the following code in the controller:
public function index(OpenRegDepDataReportInterface $openRegDepDataReport, Request $request): Renderable
{
$applications = $openRegDepDataReport->getReport($advertisers, $category);
return view('applications.index', compact('applications'));
}
So, the method getReport gives me a result of DB::select('<here is my big diffecult SQL>'), and, as well known it's an array.
But as you can see I'm trying to pass the result on a view. And, of course, I would like to call $applications->links() in the view, like for eloquent collection. Which is proper and faster way to do that?
doc
To display pagination at the table, you must call the select and then the pagination method.
in Controller:
$test = DB::table('users')->select('id')->paginate(10);
in View:
$test->links();
So based on the documentation if your $applications returns a Query Builder result, then just append ->paginate(10); to it.
https://laravel.com/docs/master/pagination#paginating-query-builder-results
$applications = $openRegDepDataReport->getReport($advertisers, $category)->paginate(10);
Simple answer, use paginate() method:
$basicQuery = DB::select(DB::raw("<here is the big diffcult SQL query>"));
However, paginate() works only on collections, and since you have an array of objects, you need to turn it into a collection first, using the forPage() method:
The forPage method returns a new collection containing the items that would be present on a given page number. The method accepts the page number as its first argument and the number of items to show per page as its second argument:
$collection = collect($basicQuery);
$chunk = $collection->forPage(2, 3);
$chunk->all();
Complicated answer: build a paginator instance yourself:
$perPage = 10;
$page = $request->input("page", 1);
$skip = $page * $perPage;
if($take < 1) { $take = 1; }
if($skip < 0) { $skip = 0; }
$basicQuery = DB::select(DB::raw("<here is the big diffcult SQL query>"));
$totalCount = $basicQuery->count();
$results = $basicQuery
->take($perPage)
->skip($skip)
->get();
$paginator = new \Illuminate\Pagination\LengthAwarePaginator($results, $totalCount, $take, $page);
return $paginator;
I would recommend using the paginate() method.
You can read more about Laravel's pagination.

Laravel simplePaginate() for Grouped Data

I have the following query.
$projects = Project::orderBy('created_at', 'desc');
$data['sorted'] = $projects->groupBy(function ($project) {
return Carbon::parse($project->created_at)->format('Y-m-d');
})->simplePaginate(5);
When I try to paginate with the simplePaginate() method I get this error.
stripos() expects parameter 1 to be string, object given
How can I paginate grouped data in this case?
The created_at attribute is already casted as a Carbon Object (by default in laravel models). that's why you are getting that error. Try this:
$projects = Project::orderBy('created_at', 'desc')->get();
$data['sorted'] = $projects->groupBy(function ($project) {
return $project->created_at->format('Y-m-d');
})->simplePaginate(5);
this answer is just for the error you're getting. now if you want help with the QueryBuilder, can you provide an example of the results you're expecting to have and an example of the database structure ?
The pagination methods should be called on queries instead of collection.
You could try:
$projects = Project::orderBy('created_at', 'desc');
$data['sorted'] = $projects->groupBy('created_at');
The problem was solved. I was create custom paginator via this example:
https://stackoverflow.com/a/30014621/6405083
$page = $request->has('page') ? $request->input('page') : 1; // Use ?page=x if given, otherwise start at 1
$numPerPage = 15; // Number of results per page
$count = Project::count(); // Get the total number of entries you'll be paging through
// Get the actual items
$projects = Project::orderBy('created_at', 'desc')
->take($numPerPage)->offset(($page-1)*$numPerPage)->get()->groupBy(function($project) {
return $project->created_at->format('Y-m-d');
});
$data['sorted'] = new Paginator($projects, $count, $numPerPage, $page, ['path' => $request->url(), 'query' => $request->query()]);
simplePaginate Method is exist in the path below:
Illuminate\Database\Eloquent\Builder.php::simplePaginate()

check count in one to many relation in laravel

I have products table that each of the product has many orders,product.php
public function orders()
{
return $this->hasMany(Order::class,'product_id');
}
I can get products that order by order_count with this code:
$products = Product::withCount('orders')->orderBy('orders_count', 'desc')->get();
Now I want to get products in controller that their orders count is bigger than 5,
How I can do it?
One way to do this would to use whereHas():
$products = Product::withCount('orders')
->whereHas('orders', null, '>', 5)
->orderBy('orders_count', 'desc')
->get();
I don't think it's mentioned in the docs but you don't have to pass a closure as the 2nd param, and the 3rd and 4th can be used to pass the operator and the count.
Alternatively, you could use having() instead:
$products = Product::withCount('orders')
->having('orders_count', '>', 5)
->orderBy('orders_count', 'desc')
->get();
You can use mapping on your collection to filter out records e.g. :`
$collection = collect($products)->map(function ($product) {
return $product->orders->count() > 5;
})
->reject(function ($name) {
return $product->orders->count() < 5;
});`
This is just an example, you can put more conditions if required.
Hope this helps.
The below code should give you just the orders with a count greater than 5
$products = Product::withCount('orders')->where('orders_count', '>', 5)->orderBy('orders_count', 'desc')->get();
Adding ->where() should do it.
Hope that helps.
Update
Try
->whereRaw('count("orders.id") > 5')
This should work to get products with more than 5 orders.

Laravel Add Additional Rows to an Eloquent Object

I want to add an "On This Day" feature which should display records from The Previous Years. I have some Entries, all of them have a 'date' attribute. This is what I've been trying so far:
public function filterByDay($id){
$entries = Entry::where('id', $id)->get();
$currentDay = $entries[0]->date;
$oldestYear = Entry::orderBy('date','asc')->first()->date;
$previousYear = $currentDay;
while($previousYear >= $oldestYear ){
$previousYear = $currentDay->subYear();
$entries->push(Entry::where('date', $previousYear)->get());
}
return view('home')->with(compact('entries'));
}
I must send a Collection of "Entry" type from this controller method so that I can use $entry->title etc in the view. But whenever I'm using $entries->push(...) , I'm getting a Collection instance, not Entry instance. How can I convert the Collection back into Entry instance? Or what is the alternative? I'm using Laravel 5.5. Some help will be much appreciated.
You can combine whereDay, whereYear and whereMonth methods to achieve it in one liner:
$entries = Entry::where('id', $id)->get();
$today = Carbon\Carbon::now();
$oldestYear = Entry::orderBy('date','asc')->first()->date;
$allEntries = Entry::whereDay('date', $today->day)
->whereYear('date', '>=', $oldestYear)
->whereMonth('date', $today->month)
->get();
return view('home')->with(compact('allEntries'));

laravel 4 paginate collection

I cant create a proper pagination system using laravel 4. I have the following models and function that return collections:
Model Restaurant:
public function fooditem()
{
return $this->hasMany('Fooditem','rest_id');
}
public function get_rest_foods($id){
return Restaurant::find($id)->fooditem->toArray();
}
The second function returns all food items for a certain restaurant as an array. I also use this in an API call.
in my controller i have this:
$food = $food->get_rest_foods($id);
$paginator = Paginator::make($food, 10, 5);
I pass the paginator to the view and it shows the links ok but also shows all my item from the food array.
I tried using
public function get_rest_foods($id){
return Restaurant::find($id)->fooditem->paginate(5);
}
but i get an error:
FatalErrorException: Error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
I searched this and many other sites but cant understant how to paginate a collection.
Thanks for your help
The paginator must get the items that it would normally get from a database query with an offset/limit statement.
So when you have a collection with all items, you should do the offset/limit yourself.
$food = $food->get_rest_foods($id);
$page = 1;
if( !empty(Input::get['page']) ) {
$page = Input::get['page'];
}
$perPage = 15;
$offset = (($page - 1) * $perPage);
$food = Paginator::make($food->slice($offset,$perPage, true)->all(), $food->count(), $perPage);
I created a subclass of Collection and implemented my own paginate method
public function paginate($perPage) {
$pagination = App::make('paginator');
$count = $this->count();
$page = $pagination->getCurrentPage($count);
$items = $this->slice(($page - 1) * $perPage, $perPage)->all();
$pagination = $pagination->make($items, $count, $perPage);
return $pagination;
}
The Laravel paginator does not do any of the splicing for you. Typically the paginator should be used hand in hand with Eloquent/Fluent. In your second example you should be doing it like this.
return Restaurant::find($id)->fooditem()->paginate(5);
Without calling the actual method you'll just be getting a collection of results and not the query builder instance.
If you want to use the paginator manually you need to pass in the spliced array (the correct items for the current page). This usually involves determining the current page, the total results and total pages. That's why, where possibly, it's best to use it with Eloquent or Fluent.

Resources