Čamo
Čamo

Reputation: 4170

Laravel duplicates join on production

I have a code where Eloquent query builder joins the ManyToOne tables articles and users. The code looks like:

$model = Article::with('user')->select('articles.*'));

and then the model filters the user name

$model = $model->join('users', 'articles.user_id', '=', 'users.id')
                ->where('users.name', 'like', "%$value%");

This throws me an error:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users' (SQL: select count(*) as aggregate from articles inner join users on articles.user_id = users.id inner join users on articles.user_id = users.id where users.name like %may% and users.name like %may% and articles.deleted_at is null)

Look at the duplicate inner join on users table and also duplicate where clause.

The same code on localhost works fine and create sql:

select count(*) as aggregate from `articles` inner join `users` on `articles`.`user_id` = `users`.`id` where `users`.`name` like '%may%' and `articles`.`deleted_at` is null

Original code is here: https://github.com/camohub/laravel-datagrid-example/blob/master/app/Http/Controllers/DefaultController.php#L25

and the live error is here: https://laravel-datagrid.tatrytec.eu/?chgrid-filter-username=may&chgrid-perPage=25

I dont understand. It looks like some database setting is wrong. Hope somebody knows what happened there. Thanks a lot.

EDIT: The issue is caused by PHP version. Production is lower 7.4.3 than localhost 7.4.19

Upvotes: 0

Views: 186

Answers (1)

Čamo
Čamo

Reputation: 4170

The solution is to write an envelope above the Eloquent query builder. Here it is https://github.com/camohub/laravel-datagrid/blob/master/src/QueryBuilder.php

Upvotes: 0

Related Questions