Closed jualoppaz closed 6 years ago
I have just done a pull request with my proposal: https://github.com/hoyvoy/laravel-cross-database-subqueries/pull/2
@jualoppaz Thanks for contributing. I will review your request to see the best way to add functionality.
@jualoppaz You need to update at 5.6.1 version to have subquery prefix support.
Thanks!
Hi Mario!
I can see you didn't add the alias in the code. I added it because the relationship in my model was called user, meanwhile the table name is wp_users.
My model is like this:
class Client extends BaseEntity{
//...
protected $table = 'clients';
public function user(){
return $this->belongsTo('Backoffice\User\User', 'user_id', 'id');
}
//...
}
If I make a query with this:
$query = $query
->whereHas('user', function($q) use ($email){
$q->where('user_email', 'like', '%' . $email . '%');
});
Eloquent throws the next error:
Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select count() as aggregate from
clients
where exists (select fromwordpress
.wp_users
whereclients
.user_id
=users
.id
anduser_email
like %email@gmail.com%
I supose that Eloquent uses the relationship name for build the table access in the query, so the prefix is not used in where clause.
I think there are three ways for avoid this error:
Can you think of a better option?
Thank you sou much.
@jualoppaz It's now released on 5.6.3. Only withCount using prefixes is not supported.
Hi!
I have used your library recently and I only miss a little feature. An example of my project subqueries is the next:
However, my users table is a WordPress table, which requires the prefix property in database connection:
So I must indicate the prefix inside the table name in subquery.
With Eloquent native version my subquery was like this:
And now with your library I only can avoid indicating the connection:
If were possible using prefix property of database connection it would be fantastic :)