Group statement being added to query automatically in Laravel - php

So I'm using Laravel eloquents to access data from my database - it's very similar to other statements in my code which work fine, but for some reason this isn't working:
$data->leftjoin('val_keys', function($leftJoin) use (&$key_words)
{
$leftJoin->on('val_keys.val_id', '=', 'vals.id');
})->whereRaw(DB::raw(sprintf('(LOWER(val_keys.key) LIKE "%%%s%%")', strtolower($key_words))));
I keep getting the following error, which shows that there is a group statement that is somehow being added to my SQL query despite the fact that I'm not putting it there:
Integrity constraint violation: 1052 Column 'id' in group statement is ambiguous (SQL: select count(*) as aggregate from `vals` left join `val_keys` on `val_keys`.`val_id` = `vals`.`id` where (`category_id` = 3780) and (LOWER(val_keys.key) LIKE "%hey%") group by `id`)
I've tried adding my own groupBy() to my query, with vals.id as the input, hoping that this would override the group statement that uses id, but it doesn't:
Integrity constraint violation: 1052 Column 'id' in group statement is ambiguous (SQL: select count(*) as aggregate from `vals` left join `val_keys` on `val_keys`.`val_id` = `vals`.`id` where (`category_id` = 3780) and (LOWER(val_keys.key) LIKE "%hey%") group by `vals`.`id`, `id`)
How do I get rid of this group statement so my query will run as intended? I've already tried changing strict to true. I am using Laravel version 5.2.
Thanks

Related

How to using Eloquent groupBy and orderBy in laravel?

\App\Service::groupBy('type')->orderBy('id', 'DESC')->get();
I am using laravel 5.5..
Can help me for resolve this problem..?
Error report
"SQLSTATE[42000]: Syntax error or access violation: 1055 'db_name.tbl_name.id' isn't in GROUP BY (SQL: select * from tbl_name group by type order by id desc) "
If you got this error
SQLSTATE[42000]: Syntax error or access violation: 1055 'tablename.fieldname' isn't in GROUP BY (SQL: select * from `tablename` group by `fieldname`)
Then edit your database config file config/database.php
In mysql array, set strict => false to disable MySQL's strict mode
When grouping in MySQL you can only work with either fields you are grouping by or fields used in aggregate functions of your select. Therefore orderBy('id', 'DESC') will do nothing in your case.

Select all columns but distinct from database table in Laravel

Bellow SQL command runs perfectly :
select * from `product` group by `owner_name` order by `id` asc
When I translate above code in my Laravel project to get the same result :
Product::select('*')
->orderBy('id','asc')->groupBy('owner_name')
->get();
This laravel code returns me error that
SQLSTATE[42000]: Syntax error or access violation: 1055
'db1.product.id' isn't in GROUP BY (SQL: select * from product group
by owner_name order by id asc)
Problem is I have many duplicated records with slight differences on some of their columns. I need to get them by owner_name and only one time .
Edit your applications's database config file config/database.php
In mysql array, set strict => false to disable MySQL's strict mode
You have don't need to do 'select(*)', by default it will select all columns data.
Try this:
Product::orderBy('id','asc')->groupBy('owner_name')
->get();
And if you want to fetch selected column you can do like this:
Product::select(['column_1', 'column_2'])
->orderBy('id','asc')->groupBy('owner_name')
->get();

Laravel: Paginating a query on a view MySQL Error 1140 Mixing of GROUP columns

I am trying to do a simple pagination of a query that uses a view:
DB::table('vw_myview')->paginate(50)
And get this 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 (SQL: select count(*) as aggregate from vw_myview)
Yet when I manually run the query, it works just fine:
mysql> select count(*) as aggregate from vw_myview;
+-----------+
| aggregate |
+-----------+
| 776 |
+-----------+
1 row in set (0.20 sec)
After searching around I found that I should just have to add a GROUP BY clause (just like the error says) and I have tried doing that with no luck:
DB::table('vw_myview')->groupBy('column_on_view')->paginate(50)
SQLSTATE[42000]: Syntax error or access violation: 1055 'database_name.table_used_by_view.column_on_table' isn't in GROUP BY (SQL: select count(*) as aggregate from vw_myview group by column_on_view)
Okay lets try grouping by a column on one of the tables the view uses:
DB::table('vw_myview')->groupBy('table_used_by_view.column_on_table')->paginate(50)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'database_name.table_used_by_view.column_on_table' in 'group statement' (SQL: select count(*) as aggregate from vw_myview group by database_name.table_used_by_view.column_on_table)
So why does the query work when I run it manually but fails when run through Eloquent? Any help would be greatly appreciated.
You know, I am thinking your Mysql server has the sql_mode = ONLY_FULL_GROUP_BY turned on... Can you check this... (from mysql ... select ##sql_mode; If it is set, turn it off... and try your query again... I think it shall pass... Mysql 5.7xxx has this turned on by default now...

Laravel SQL error with orderBy and count

I've suddenly started getting a weird error in my production environment with count(). The following two statements work totally fine:
User::where('created_at', '2017-06-22 18::33:54')->count()
User::where('created_at', '2017-06-22 18::33:54')->orderBy('last_name')
but if I try
User::where('created_at', '2017-06-22 18::33:54')->orderBy('last_name')->count()
I get the following error:
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 count(*) as aggregate from users where created_at = 2017-06-22 18::33:54 and users.deleted_at is null order by last_name asc)'
This doesn't happen in my local environment, and I'm not sure where to look for an answer in production.
Thoughts?

Getting the Query Builder object behind fetching a relation in Laravel

We have modeled self-referencing relations in our User model using belongsToMany(). Users can be agent or seller for each other - so we definer seller() and agents()
Now we are using https://github.com/Nayjest/Grids which needs a query to the grid.
In the grid we want for example to display a seller's agents.
Currently we are using a hand-crafted query for that - but we would like to pull the logic from the model.
So what we need is the query (as query builder object) which is executed when fetching
$seller = Auth::user(); // or any other instance of User
$seller->agents
We tried
$query = $seller->newQuery()->where('laravel_reserved_1.id', '=', $seller->id);
return $user->agents()->getRelationQuery($query, $query);
without luck.
EDIT
Trying $seller->agents()->getQuery() we get
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous
(SQL: select * from `users` inner join `user_connections` on
`users`.`id` = `user_connections`.`user_id`
where `user_connections`.`related_user_id` = 2 and
`user_connections`.`type` = agent order by `id` asc
limit 15 offset 0)
Turns out it is just
$user->agents()->getQuery()
We had a column "id" in the grid which led to the ambiguous query later in the process.

Categories