Join table multiple columns groupBy with count in Laravel - laravel

I'm trying to group joindraw table with count, but getting the following error. How can i get the count and group via multiple columns?
Controller
$joindraw_participants = Joindraw::where('product_id', $id)
->groupBy('user_id', 'created_at')
->with('user')->take(10)->desc()->get();
Error
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yangmao.joindraw.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from joindraw where product_id = 1 group by user_id order by joindraw.created_at desc limit 10)

With strict mode enable groupBy needs all the columns in the select to be in groupBy also. To remove this validation either you can set strict => false in config/database.php for mysql or you can specify the modes so that other modes won't be affected. Like this:
'modes' => [
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
]

Put an empty array of modes to overwrite the default in your config/database.php file.
'mysql' => [
'modes' => [],
],

Related

how to convert the sql to QueryBuilder in Laravel

$old_records = DB::connection("mysql_old_s")->table('leads')
->select(DB::raw('count(*) as source_count, source'))
->groupBy('source')
->get();
dd($old_records); // works right
This query works right.
I want to add one more column like id or created_at to select
I couldn't do it as following:
$old_records = DB::connection("mysql_old_s")->table('leads')
->select('id',DB::raw('count(*) as source_count, source'))
->groupBy('source')
->get();
dd($old_records); // it gives an error
It gives me an error that is:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP
BY clause and contains nonaggregated column 'crmclini_crmv2.leads.id' which is not functionally
dependent on columns in GROUP BY
clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `id`, count(*) as
source_count, source , id from `leads` group by `source`)
you should include the columns you select within your group by clause to group by them:
$old_records = DB::connection("mysql_old_s")->table('leads')
->select('id',DB::raw('count(*) as source_count, source'))
->groupBy(['id','source'])
->get();
the second option (witch I do not recommend), you can disable only_full_group_by option in config/database.php:
'connections' => [
...
'mysql' => [
...
'strict' => false,
...
],
]
unless you have special condition, make the query group by the columns you select

Laravel Eloquent Select Group By : column "id" must appear in the GROUP BY clause or be used in an aggregate function

Grouping error: 7 ERROR: column "id" must appear in the GROUP BY clause or be used in an aggregate function
i want to show any id after I did the GroupBy multiple Column. even if the id is from first or from last
my code is :
$data = Book::select(
'id',
'tittle',
'writer',
)
->groupBy('tittle','writer',)
->paginate(10);
Remove Id column from select function.
$data = Book::select(
'tittle',
'writer',
)
->groupBy('tittle','writer',)
->paginate(10);
Beacuse grouping is done when we have a duplicate record in our tables but id is primary key so remove it and than try again hope it will work

Postgres groupBy not working with Laravel Backpack

I am using Laravel Backpack with Postgres. I am wanting to groupBy (or distinct?) by a column to limit the list view. I have a single table that I want to groupBy thread_id. My latest attempt has been something like this:
$this->crud->groupBy('thread_id');
That code returns the error:
message: "SQLSTATE[42803]: Grouping error: 7 ERROR: column "messages.created_at" must appear in the GROUP BY clause or be used in an aggregate function
(SQL: select count(*) as aggregate from "messages" where "to_user_id" = 2 or "from_user_id" = 2 group by "thread_id" order by "thread_id" desc, "created_at" desc)"
Has anyone ran into this issue, specifically with Backpack?

Laravel unable to update field to null

I can't figure out how to set a field to null on certain condition. It should be an easy query but I'n not able to set the right value for NULL on a nullable field.
Product::where('id', $product_list)->update(['family_id' => null]);
nor
Product::where('id', $product_list)->update(['family_id' => DB::raw(null)]);
did the trick.
Both cases compiled fine but the generated query seems to be wrong because I get a SQL error. In the irst case the error is :
SQLSTATE[HY093]: Invalid parameter number (SQL: update `products` set `family_id` = ?, `products`.`updated_at` = 2019-11-12 08:41:07 where `id` = ?)
and the error for the second one is :
QLSTATE[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 ', `products`.`updated_at` = ? where `id` = ?' at line 1 (SQL: update `products` set `family_id` = , `products`.`updated_at` = 2019-11-12 08:21:50 where `id` = ?)
In the first case it seems that NULL is not being added to the parameters to the prepared query, and in the secon the NULL value is being inserted as nothing in the query. There is any simple way to set the value to NULL?
You should consider the following things first.
1) family_id column is nullable in your table. if not then set nullable.
2) Make sure you have in $fillable property of Product model column family_id.
Make sure $product_list returns id of that particular product.
if $product_list is collection/array of product data then use like:
Product::where('id', $product_list->id)->update(['family_id' => null]);
Try this
Product::where('id', $product_list)->update(['family_id' => 'NULL']);

Equivalent ORM Query in Laravel

Following is my query:
select nonrem_id as RefID , (SELECT group_concat(concat(nonremu_updated_date, ' - ',name,' - ',ttu.nonremu_updates) SEPARATOR ',') FROM `fanda_bank_nonremit_update` as ttu left join fanda_mst_users on login_id = nonremu_createdby WHERE ttu.nonremu_cid = fanda_bank_acc_nonremitted.nonrem_id order by ttu.nonremu_id asc ) as Comments from `fanda_bank_acc_nonremitted` left join `fanda_mst_users` on `login_id` = `nonrem_created_by` where `nonrem_gl_date` >= 2017-02-01 and `nonrem_gl_date` <= 2017-05-11 and `nonrem_country` = BE
How do I write this query in Laravel Eloquent ORM. I have tried using Query builder, but I wanna know how to write this query in pure ORM.
P.S: I should not use DB::raw() anywhere.
Kindly help.
You can't, you'll need DB::raw() to build a select statement like that.
EDIT:
If you don't care about using DB::raw() on your select, then try something like this on your select:
Model::select('nonrem_id as RefID',DB::raw('(SELECT group_concat(concat(nonremu_updated_date, ' - ',name,' - ',ttu.nonremu_updates) SEPARATOR ',') FROM `fanda_bank_nonremit_update` as ttu left join fanda_mst_users on login_id = nonremu_createdby WHERE ttu.nonremu_cid = fanda_bank_acc_nonremitted.nonrem_id order by ttu.nonremu_id asc ) as Comments'))
If you are using strict mode it will throw a QueryException cause you are ordering your first query by a non selected field , to avoid this query exception configure your db connection to allow this kind of order by. You can do it on the file: config/database.php by adding this two parameters to your actual connection within your 'connections' array:
'strict' => true,
'modes' => ['STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'],

Resources