Using with and withCount together in laravel eloquent - php

withCount is a new method of laravel to return count of the relations. I'm trying to use with and withCount together. For example:
Article::with('Comments')->withCount('Comments')->paginate();
Problem I'm facing is, in the results is see for example:
comments_count = 10
comments = []
It returns comments array null. I don't know is it possible to get both the results or not. In some articles on different sites, i see withCount still has some restrictions. But not sure what I'm trying to do is possible or not.
Please help me to know about this issue.
Thank you,

No Need to write with.
Article::withCount('Comments')->paginate();

Related

How to implement pagination along with sorting results in Laravel

I'm pretty new to Laravel, and still exploring it's features and possibilities, and I've came across a strange issue that I don't quite understand.
First of all, I'm using Laravel 5.0 and I'm trying to make pagination of results that came from model alongside with sorting them.
Here is the code that works, but without sorting:
News::paginate(5);
And here is what I need, but doesn't work:
News::paginate(5)->orderByDesc('published_at');
I've tried doing it like so:
News::all()->sortByDesc('published_at')->paginate(5);
But then, when I call render() method inside view it throws error. I would like to use model's approach since it's handy and few more things depend on it, and not query builder method. Also, would like, if possible to avoid manual or any kind of custom pagination. Thanks
You should write this. This will solve your problem
News::orderBy('published_at','desc')->paginate(5);

Laravel 5 Model chaining

Assume A,B,C,D are models. Is there a way to cleanly chain models like so: a->b()->c()->d()->get();? When trying to do this, I get an error since a->b(), b->c(), and c->d() all return sets and not a single object.
Some people have suggested eager loading in other sites, but I have no idea how to use them. So far I have tried using a->load('b.c.d'); in hopes of loading all the models to 'a' but it didnt work.
How can I load all relevant models b,c,and d to a?
You can use with
$v = a->with('b.c.d')->get();
then you can use in your code like this
$v->b->c->d

apigility - What alternatives I can use for Zend\Paginator\Paginator in Collections?

In several endpoints I'm doing with Apigility, I want retrieve all the possible results without pagination. Is there a class to replace Paginator in Collection?
To receive a response in Hal without pagination you should simply return an array from your queries instead of an instance of Zend\Paginator\Paginator.
For those who happen to find this question just as I did. You can edit the number of items per page in the module.config.php file. It's set as the value "page_size". Changing it to a number larger than the total amount in your collection will let you receive all the results in one page.
Admittedly not the cleanest of solutions, but it worked for me.

Laravel ORM - where, with and paginate

can someone help me with the following laravel ORM questions
I have 2 models ProductGroup and Product. ProductGroup hasMany Products and is linked via productGroup.product_id and product.id
I want to return a set of results with pagination. Quite simple wish, isn't it?
My current code is
$result = ProductGroup::where("name", "=", "laptop")
->with(array("product"=>function($q){
$q->select("id","name");
}))
->get(array("id","product_id","name","created_timestamp"));
It works but I cannot add ->paginate() otherwise it will throw the following error
Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
What is the best way of doing the pagination? I can create the pagination manually (via Paginator::make )but I don't think it's good because it still returns the whole set of data (slowing down the traffic). I can use raw query to limit the result but it is also troublesome because I need to again manually calculate the total result
I am using Laravel 4.2 with MySQL
Thanks for any help
I found a solution, which could help others
Wrong way
->get((array("id","product_id","name","created_timestamp"))
->paginate(3)
Correct way
->paginate(3, (array("id","product_id","name","created_timestamp"))
Feel free to downvote it if it is wrong

CakePHP Paginate() the results from a find subquery?

This is a follow-up to an earlier question here: CakePHP find statement for a sql query involving two models
I wasn't sure whether to add it on to there, but I figured this is a new question in itself. My apologies if this isn't correct!
Given a cakephp find subquery, is it possible to use paginate() on the results of the find? from the cake documentation, it seems that the paginate() function only works on Models, not on an array of data.
Thank you!
if you already have all the data in the array, I suggest output all that to your page (not the same as showing all the data) and use javascript to paginate it (just use a javascript grid plugin out there): more responsive app and less load on the server.

Categories