Postgres groupBy not working with Laravel Backpack - laravel

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?

Related

can laravel eloquent models do aritmitic on select statments?

Is it possible to do selection from the database similar to this sql statment with laravel? Or will I have to add a win_percent column and do the calculation manually when adding new entries to the database?
select *, wins / (wins + losses) AS win_percent

Laravel Paginate with OrderBy

When querying a product code table I have the following
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10);
This works fine on the initial load of 10 records but when a subsequent call is made for page 2 I get the following error
Incorrect syntax near 'offset'. (SQL: select * from [stock_records] order by [stk_physical] desc offset 10 rows fetch next 10 rows only)
I'm using Laravel 8.0 with SQL
You should append query string to the pagination like this
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10);
$results->appends(["order_by" => "stk_physical"]);
This will append the &order_by=stk_physical to each link in the view and you can also use withQueryString() to take in consideration query string in future pagination like this
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10)->withQueryString();

Laravel Group data by Month Query works with MySQL but Fails on PostgreSQL database

I'm trying to group data by month for the current year. The query below works on MySQL database but gives an error when connected to PostgreSQL.
$results = DB::table('visits')
->select(DB::raw('month(visitdate) month, count(*) data'))
->where('team_id', 2)
->whereYear('visitdate', now()->year)
->groupBy('month')
->get();
This error was generated:
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "month" LINE 1: select month(visitdate) month, count() data from "visits" w... ^ (SQL: select month(visitdate) month, count() data from "visits" where "team_id" = 2 and extract(year from "visitdate") = 2021 group by "month")
I tried a similar query with Eloquent and got the same results.
The visitdate column stores the date in the format "2021-01-01"
I'm using laravel 8 with Breeze
month is a keyword. You should choose something else instead, or put it in double quotes, or in this case just put AS in front of it. Any of the three should work.

date method in eloquent querybuilder

i have this raw query and i want to use it in eloquent query builder
but seems i cant use date method in eloquent and gave me this error im new in eloquent. what is the problem:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'date(prizes.created_at), user_id' in 'group statement' (SQL: select user_id,COUNT(user_id), date(created_at) from `prizes` group by `date(prizes`.`created_at), user_id` having `user_id` = 1 order by `date(created_at)` desc)
raw SQL:
SELECT
user_id,COUNT(user_id), DATE(created_at) FROM prizes
GROUP BY DATE(prizes.created_at), user_id
HAVING user_id = 1
ORDER BY DATE(created_at) DESC
limit 2
Eloquent:
$points = \App\Prize::selectRaw('user_id,COUNT(user_id), date(created_at)')
->groupBy("date(prizes.created_at), user_id")
->orderBy("date(created_at)","DESC")
->having("user_id","=",1)
->get();
what is the the cleanest and best form??
By default, Laravel tries to parse all strings as tables. This means it will add the string between `.
To avoid this, you can put the string in a DB:raw() function to let Laravel know not to parse this string and send it as-is to the database.
->orderBy(\DB::raw("date(created_at)"), "DESC")
Or use the raw method for ordering:
->orderByRaw('date(created_at) desc')

Oracle - Update COUNT of rows with specific value

This question has been posted several times but I am not able to get it work. I tried the approach mentioned in Update column to the COUNT of rows for specific values in another column. SQL Server. It gives me SQLException: java.sql.SQLException: ORA-01427: single-row subquery returns more than one row error not sure what I can do.
Below is my problem
UPDATE dataTable
SET ACCX = (select b.cnt
from dataTable a
join
(SELECT Account,
COUNT(1) cnt
FROM dataTable
GROUP BY Account) b
on a.Account=b.Account)
,ACCR = 15481
,ACCF = 3
WHERE ID = 1625
I only have the access & can change to the bold part since rest of the query is generated by the tool I cannot change it & I have to update ACCX column with the count of the value in column Account. Is it possible to do?
Note: - Account column is already populated with values.
You cannot follow that query because it is for sqlserver and NOT oracle. It is simpler in oracle and does not need a join to itself.
This update will set the count for id 1625 only based on number of account numbers in datatable. See demo here;
http://sqlfiddle.com/#!4/d154c/1
update dataTable a
set ACCR = 15481,
ACCF = 3,
a.ACCX = (
select COUNT(*)
from dataTable b
where b.Account=a.Account)
WHERE a.ID = 1625;

Resources