Laravel duplicates join on production - mysql

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

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

Related

How to resolve ambiguous error in the query

I am using laravel framework for developing API's ,i have one query that is executed without where condition without any error i need to execute with where condition but it's throwing an error
query
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `access_id` = 5054
SQL Error [1052] [23000]: Column 'access_id' in where clause is
ambiguous
after searching google i got something we have to specify reference of a table name , i added reference name like this where users.access_id =5054 but it's throwing an error like unknown column but in my db i have that column in both users and books table
The problem is its consider as a column so that's why syntax error is coming,try following way it will resolve your problem
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `users`.`access_id` = 5054

Laravel7 Join Syntax error or access violation

I am trying to write a query where I am joining a messages table and a users table so I can get all messages for a specific user and get the name of the user each message was from.
Here is my query
$inboxrow = DB::table('inbox_messages')
->join('inbox_messages', 'inbox_messages.to_userid', '=', Auth::id())
->join('users', 'users.from_userid', '=', 'inbox_messages.from_userid')
->select('inbox_messages.*', 'users.name')
->get();
My issue is the above gives me an error (see below) and not sure how to re-write my query to get what I need. Which is everything from the inbox_messages table where the to_userid equals the logged in persons userid and then get the name of the person who sent the message from the users table where the value of the column inbox_messages.from_user = the value from the column users.id
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias:
'inbox_messages' (SQL: select `inbox_messages`.*, `users`.`name` from `inbox_messages`
inner join `inbox_messages` on `inbox_messages`.`to_userid` = `3` inner join `users`
on `users`.`from_userid` = `inbox_messages`.`from_userid`)
Please, try with the below solution. Where I change extra join to where clause.
$inboxrow = DB::table('inbox_messages')
->join('users', 'users.from_userid', '=', 'inbox_messages.from_userid')
->select('inbox_messages.*', 'users.name')
->where('inbox_messages.to_userid', '=', Auth::id())
->get();

converting from simple mysql to Laravel query constructor

#forpas guys, I need to the query into laravel constructor.
this is what I tried.
$cates = DB::table('categories')
->select(DB::raw('categories.category_id, categories.category_title, categories.created_at, COUNT(task.task_id) AS counted_tasks'))
->leftJoin('tasks', 'categories.category_id', '=', 'tasks.task_cat_id')
->get();
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select categories.category_id, categories.category_title, categories.created_at, COUNT(task.task_id) AS counted_tasks from categories left join tasks on categories.category_id = tasks.task_cat_id)
Where I can see the final query of the constructor?
You're missing the groupBy. Try this code below:
$cates = DB::table('categories')
->select(DB::raw('categories.category_id, categories.category_title, categories.created_at, COUNT(task.task_id) AS counted_tasks'))
->leftJoin('tasks', 'categories.category_id', '=', 'tasks.task_cat_id')
->groupBy('categories.category_id, categories.category_title, categories.created_at')
->get();

Symfony2 - subquery within a join error

I would like to use a subquery inside a join, however Symfony2 throws the following error:
Here is my failed attempt:
$query = $em->createQuery(
'SELECT
sc.id AS id,
u.id AS userId,
u.username AS username,
sc_count.upvotes
FROM
myBundle:SuggestedCar sc
INNER JOIN myBundle:User u WITH sc.user_id = u.id
INNER JOIN ( SELECT sc1.user_id, COUNT(sc1.id) AS upvotes
FROM myBundle:SuggestedCar sc1
GROUP BY sc1.user_id
) sc_count WITH u.id = sc_count.user_id'
);
Basically I'm just joining 3 tables and the third one has a count. The query worked when executing it inside the database.
How would it be possible to use a SELECT statement inside a join? Is it a good idea to use raw SQL at this point?
The $em->createQuery() function is expecting DQL as the parameter, not SQL. If you want to execute a raw SQL statement, the syntax is different. You can do it like this:
$sql = "SELECT * FROM my_table";
$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
for more on DQL or querying for objects, see Querying for Object. The biggest difference is DQL will return an object (based on your entity classes in Symfony). The method I posted above will just give you a PDO result. So if you execute raw SQL, don't expect to be able to use the result as an object.
If you want to use raw SQL and still have the result mapped to an object, you can look at the doctrine docs about Result set mapping. In my opinion, this is more work than necessary.

Zend_Db_Select - Joins and Count - Possible Zend Bug?

I'm having an issue getting a COUNT() from a SQL query using Zend_Db_Table_Select, and I think it may be a possible bug because the SQL it should be generating actually works. Here's the Zend Select Query: ($this is a Zend_Db_Table, renamed to table1 in this example)
$select = $this->select();
$select->setIntegrityCheck(false);
// Select Count
$select->from($this, array("COUNT(*) as 'COUNT'"))
->joinLeft('users', 'table1.userID = users.userID')
->joinLeft('table2', 'users.anotherKey = table2.anotherKey');
// Add Where clause after join
$select->where('users.anotherKey = ?', $anotherKeyValue);
This gives the error:
SQLSTATE[42000]: Syntax error or access violation: 1140
Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is
illegal if there is no GROUP BY clause`
However, this query...
SELECT COUNT(*) AS 'count' FROM table1
LEFT JOIN users ON table1.userID = users.userID
LEFT JOIN table2 ON users.anotherKey = table2.anotherKey
WHERE users.anotherKey = [anotherKeyValue]
...returns the expected results with no errors when run against the database. Any ideas whats going on, why the error, and how to get around it?
have you tried to see actual query, that zend_db produce?