#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();
Related
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
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();
I get 1054 - Unknown column error when I use backticks in where clause with mysql
`roles`.`id`
but it works fine with
'roles.id'
Any idea why and how to solve it? I need to make it work with backticks to make use of the Laravel Eloquent query builder.
select *
from `users` inner join `role_user` on `users`.`id` = `role_user`.`user_id`
where `roles`.`id` = `role_user`.`role_id` and (`users`.`id` = 8) and `users`.`id` = 8
Laravel - Eloquent query:
Policy::whereHas('role.users', function ($query) use ($user_id) {
$query->where([
'users.id' => $user_id
])->get();
});
As stated by #Barmar my query was missing a roles table in the FROM or JOIN clauses.
Since I have this error because the query was generated from Laravel - Eloquent I am posting my solution for future reference.
Policy::whereHas('role.users', function ($query) use ($user_id) {
$query->where([
'users.id' => $user_id
]);
})->get();
The problem was because I misplaced the get in the query builder inside whereHas instead of the outermost the query builder.
I am trying to write a query in Laravel using Eloquent, but only want the last 5-ongoing fields in it. This is the query:
public static function past_profile_fan_likes($id) {
$latest_profile_fan_likes = DB::table('fanartists')
->join('artists', 'fanartists.artist_id', '=', 'artists.id')
->orderBy('fanartists.created_at', 'DESC')
->skip(4)
->where('fanartists.fan_id', '=', $id)
->select('artists.id', 'artists.fbid', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')
->get();
return $latest_profile_fan_likes;
}
When I call this, I am getting this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'offset 4' at line 1 (SQL: select `artists`.`id`, `artists`.`fbid`, `artists`.`stage_name`, `artists`.`city`, `artists`.`state`, `artists`.`image_path`, `artists`.`description` from `fanartists` inner join `artists` on `fanartists`.`artist_id` = `artists`.`id` where `fanartists`.`fan_id` = ? order by `fanartists`.`created_at` desc offset 4) (Bindings: array ( 0 => '1', ))
Is there something I am doing wrong here? Maybe something wrong with the skip usage? Thanks for your help.
You need to add a take query in order for it to add a LIMIT query and turn it into the correct syntax;
DB::table('fanartists')
->join('artists', 'fanartists.artist_id', '=', 'artists.id')
->orderBy('fanartists.created_at', 'DESC')
->skip(4)
->take(100)
->where('fanartists.fan_id', '=', $id)
->select('artists.id', 'artists.fbid', 'artists.stage_name', 'artists.city', 'artists.state', 'artists.image_path', 'artists.description')
->get();
You will need to suppliy a limit if using an offset, even if you don't want to limit. See here: http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990
This looks more like fluent which I'm not quite as familiar with. but if you have your models and relationships defined, using Eloquent, you could reverse the order and take(5) if you're just looking to get the last 5 records:
like this...
$latest_profile_fan_likes = fanartists::with('artists')->where('fan_id', '=', $id)->orderBy('created_at', 'ASC')->take(5)->get('id', 'fbid' .......);
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?