OrderBy using withCount on Polymorphic relationship not working on Laravel 5.2 / 5.3 - laravel

I love the withCount method on Eloquent to orderBy a relationship count. Let's say we'd want to get some posts ordered by the number of favorites they have, in a "normal" one-to-many relationship:
$posts = $query->withCount('favorites')->orderBy('favorites_count', 'DESC')->get();
That works perfectly, as I said, in normal relationship. BUT when is used with a polymorphic relationship, it seems to take a huge amount of time so for more than 100 - 1000 elements it fails, in both Laravel 5.2 and 5.3.
Is there anything I'm missing? I've used polymorphic relationships quite often, I don't think that there is a problem with the relationship itself but just with the withCount method.
Note: I can't use sortBy, it must be orderBy. And I know I could do it with Fluent query builder, but I'm interested on using Eloquent this way.
Thanks!

Related

How to write this query with Laravel ORM

I am trying to write this query with laravel orm to use the eager loading but not getting it.
SELECT p.* FROM tbl_posts as p,tbl_posts_requests as pr WHERE p.post_status='active' AND (p.post_id=pr.request_post_id OR p.post_private='no' )
I have a comment table to eager load with it but i am not able to create a laravel ORM version of this query
Eloquent ORM eager loading happens for Eloquent models with specific relationships.
So assuming you have a tbl_posts_requests relationship in your Posts model, it could be something like this:
Posts::where(['post_status' => 'active', 'post_private'=>'no'])->with('tbl_posts_requests')->get();
I am assuming you called your relationship function tbl_posts_requests there.

How do the "has" and "whereHas" methods work in Eloquent?

I have a relationship defined between users and permisson, a user can have many permissions.
When I use "with" I get the data normally.
$user->with('permisson')->get();
I get the user with their permissions.
When I use "has" it only returns the user.
$user->has('permission')->get();
For what I've read, I should get the permissons if the user contains at least one permission.
I am using Postgres driver.
with is used for eager loading a relationship. You'd use it to fetch all of the specified relationships for each model (individually, or in a collection).
has is for using the relationship as a constraint or filter. As you said, using something like has('permission') will add a constraint to the query that says "only get Users that have at least one permission". This does not automatically load the relations like with(), it only creates the constraint.
You can combine the two if you want to take advantage of both the constraint and eager loading the results.
User::has('permission')->with('permission')->get();
Seems a bit redundant, I know.
From laravel docs:
If you wish to limit your results based on the existence of a relationship, you should use the has method. For example, if you want to retrieve all blog posts taht have at least one comment, yo may pass the name of the relationship to the has and orHas methods:
$posts = Post::has('comments')->get();
So, when you use the has method, it will not return the relationship, but just the post models which has at least one comment.
The whereHas and orWhereHas methods allows you to add customized constraints to a relationship constraint, such as checking the content of a comment (using the laravel example):
$posts = App\Post::whereHas('comments', function (Builder $query) {
$query->where('content', 'like', 'foo%');
})->get();
When you use the with method, you are loading the realtionship with the model:
$user->with('permission')->get()
This code will load the user and the permission relationship, while this one
$user->has('permission')->get();
will load the user who has some permission.
Hope it helps.

Laravel - How to relate two collections like Eloquent method "belongsToMany" and "with"

How can you combine two collections, one being the collection of parent items together combined with your collection of child items?
I would like something like using the method with and belongsToMany,but in this scenario I cannot use both methods correctly because one table is in another schema and the pivot table is in another schema.
Area::with('permissoes')
->where('sistema', '<>', 'S')
->get()
Most Eloquent eagerloads are done with separate queries that just use an IN statement on keys from the previous one. Pivot tables are the exception. It sounds like you need to explicitly tell the Model relation what database your pivot table is in. See my answer here: belongsToMany relationship in Laravel across multiple databases

How to use eager loading with custom query builder in Laravel

I am new to Laravel 5.4 and working on some query manipulation. Now I have created an query using query builder which is shown below:
$view = DB::table('blocks')
->leftjoin('programmes', 'blocks.programme_id', '=', 'programmes.id')
->select('blocks.id', 'blocks.programme_id', 'blocks.name', 'blocks.colour', 'blocks.year', 'programmes.title AS programme');
I have two more table "dates" and "modules". Each dates as well as module belongs to blocks.
Now I want to fetch all blocks with programmes, dates and modules. I know i can use with() method to get all of these. But as per my knowledge on Laravel, I can use with() method only if I have model file of each table and have relationship between them.
But do not want to use model and define relationship between them. I just want to know How can I get block data with programmes, dates and modules without creating model and defining relationship betwen them in model? Is there any other way to use with() method without model?
Block dates and modules are conditional, Sometime I dont want get data of this table with block.
Please help me on this.
You can't do it automatically. Eager loading is only for Eloquent model so you cannot use it with query builder. However in most cases you can use Eloquent also for getting more complicated queries (you can also use joins when using Eloquent) so you will be able to use eager loading.
But if you don't want to use Eloquent at all, obviously you will need to create some custom mechanism for eager loading.

Translate query to Laravel Eloquent Model

How would I translate the following query into a format that Laravel can parse?
select posts.id,title from posts,albums where posts.id != albums.post_id
Now it has this ORM relations
a Post hasOne Album
an Album Belongs to a Post
So with this statement I want to know which Posts dont have Albums binded.
so far I have
Post::with('album')->select('id', 'title')->get();
I don't know how to declare the condition
Please do read docs again, I think you may look for polymorphic relationship or One to Many. If you use Eloquent ORM do use relationships its the biggest why "YES".
As for the query you can do http://laravel.com/docs/4.2/queries#raw-expressions
There is an example in a link just fill in your query and you are done.

Resources