Eloquent returns empty collection same table
Consider the following:
class Post extends Eloquent
{
protected $table = 'posts';
public function children()
{
return $this->hasMany('Post', 'parent_id')
}
public function parent()
{
return $this->belongsTo('Post', 'parent_id');
}
}
So, i'm having a tree like configuration. When I have an children post and I try to get the parent of this children post, the following SQL query is returned from Eloquent.
select * from `posts` where (select count(*) from `posts` where `posts`.`parent_id` = `posts`.`id`)
which returns an empty Collection. When debugging, I found out that the Collection is empty because of the use the word posts in the query. What would work is to alias those tables, like:
select * from posts u where (select count(*) from posts p where p.parent_id = u.id)
Is there something with Eloquent we can use to solve this, or this has to be an custom query?
This should work. Could you paste code as to what you are trying to do?
@marcvdm I'm simply doing:
$posts = Post::has('parent')->get();
returns an empty collection, because it's executing
select * from `posts` where (select count(*) from `posts` where `posts`.`parent_id` = `posts`.`id`)
that also returns nothing. The query should be something like
select * from posts u where (select count(*) from posts p where p.parent_id = u.id)
instead of the first one.
I accomplished something similar to this using belongsToMany and using a hierarchy table.
public function parents()
{
return $this->belongsToMany('Entity', 'hierarchy', 'id', 'parent_id');
}
public function children()
{
return $this->belongsToMany('Entity', 'hierarchy', 'parent_id', 'id');
}
This case was used because entities could have multiple parents and/or children.
In your case I'm thinking your solution might be
class Post extends Eloquent
{
protected $table = 'posts';
public function children()
{
return $this->hasMany('Post', 'parent_id', 'id'); // the 'id' here probably isn't necessary, but just in case
}
public function parent()
{
return $this->belongsTo('Post', 'id', 'parent_id');
}
}
@riotCode I'm afraid thats not the solution, same result, an empty collection.
Interesting, with your setup
$posts = Post::has('parent')->get();
I can confirm I can not get anything. I can get a parent through the relationship though
$post = Post::find($id);
$post->parent;
@riotCode I can confirm that the second method works, I'm afraid this doesn't solve the problem.
@ceesvanegmond, yeah it doesn't they're different use cases but the relationship does work to an extent. It seems that the has() and whereHas() methods do not in this case, if we could change the Eloquent behavior to use alias like you said would give us this ability.
Hmm, might not be possbile. Never setup a relatinoship like this.
@taylorotwell Do you have some alternatives to solve this problem?
Does the relationship load? Only the has query doesn't work correctly?
@taylorotwell That is correct, the has and whereHas methods dont work. The relationship does load with the use of with or manually fetching it.
OK, might just have to work around that with your own existence check for now. I'll explore ways to get it working.
On Thu, Jun 19, 2014 at 5:09 PM, Cees van Egmond [email protected] wrote:
@taylorotwell https://github.com/taylorotwell That is correct, the has and whereHas methods dont work. The relationship does load with the use of with or manually fetching it.
— Reply to this email directly or view it on GitHub https://github.com/laravel/framework/issues/4754#issuecomment-46623557.
You could try Post::whereNotNull("parent_id")->get();
So when the parent_id is filled in it has a parent
That doesn't give the expected result, in that case it would retrieve all the posts that are children of another post. The expected result is to retrieve all posts that have children.
@riotCode He said he was doing $posts = Post::has('parent')->get(); meaning he wants all posts having a parent. So he does not want root Posts
@marcvdm that's correct, my mistake. That would be an alternative solution for his case instead of using has()
I think this is related to 4084.
Most of the relations, Illuminate\Database\Eloquent\Relations\ *, have a function getRelationCountQuery. These functions return this:
return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));
But for self referencing relations the table needs to be aliased. Something like this:
$parentTable = $this->getParent()->getTable();
$relatedTable = $this->getRelated()->getTable();
$qForeignKey = $this->getQualifiedForeignKey();
$otherKey = $this->getOtherKey();
if($parentTable == $relatedTable) {
$query->from($parentTable.' as '.$parentTable.'2');
$query->whereRaw($qForeignKey.' = '.$parentTable.'2.'.$otherKey);
return $query;
Is this fixed?
This means basically that self-joins cannot work with Eloquent, right? Isn't that a pretty common use case?