Write multiple laravel query at once - laravel

im new to laravel, I just want to know if there's any way to efficiently rewrite this code?
$answer1 = SurveyAnswer::where('answer_1','=','1')->get()->count();
$answer2 = SurveyAnswer::where('answer_1','=','2')->get()->count();
$answer3 = SurveyAnswer::where('answer_1','=','3')->get()->count();
$answer4 = SurveyAnswer::where('answer_1','=','4')->get()->count();
$answer5 = SurveyAnswer::where('answer_1','=','5')->get()->count();

Get the data first:
$answers = SurveyAnswer::whereIn('answer_1', [1, 2, 3, 4, 5])->get();
Then count answers using loaded collection:
$answer1 = $answers->where('answer_1', 1)->count();
$answer2 = $answers->where('answer_1', 2)->count();
...
This code will generate just one DB query instead of five.

Try this:
$matches = [1,2,3,4,5];
$answer = SurveyAnswer::whereId('answer_1', $matches)->groupBy('answer_1')->get();

You can easily do it with an agregate function with a case expression, since mysql doesnot support native pivoting functions
Following is a sample, and try to rewrite according to your requirement and runt it against database directly, if it works, then you can use it with laravel raw sql.
select id,
sum(case when value = 1 then 1 else 0 end) ANSWER1_COUNT,
sum(case when value = 2 then 1 else 0 end) ANSWER2_COUNT
from survey
group by answer

Related

How to take count based on different where conditions in laravel?

I am trying to count tickets based on different where conditions . For which I am using four different queries but same model. Can I do that in one query?
$openTickets = Ticket::where('status',1)->count();
$pending = Ticket::where('status',2)->count();
$unAssigned = Ticket::where('agent_id',null)->count();
$unResolved = Ticket::whereNotIn('status',[3,4])->count();
Ticket::selectRaw('COUNT(CASE WHEN status = 1 THEN 1 END) AS open_tickets')
->selectRaw('COUNT(CASE WHEN status = 2 THEN 1 END) AS pending_tickets')
->selectRaw('COUNT(CASE WHEN agent_id IS NULL THEN 1 END) AS unassigned_tickets')
->selectRaw('COUNT(CASE WHEN status NOT IN (3,4) THEN 1 END) AS unresolved_tickets')
->first();
You can of course resolve the multiple queries with this query. We can use conditional cases and count.
You can sum up conditionals but will need lots of raw parts in your query:
$result = DB::table('tickets')->whereIn('status', [1,2])->orWhereNull('agent_id')->orWhereNotIn('status', [3,4]) // This is to filter out the things we don't care about
->select([
DB::raw('SUM(IF(status = 1, 1,0)) as countOpen'),
DB::raw('SUM(IF(status = 2, 1,0)) as countPending'),
DB::raw('SUM(IF(agent_id IS NULL, 1,0)) as countUnassigned'),
DB::raw('SUM(IF(status NOT IN (3,4), 1,0)) as countUnresolved'),
])->first()
$openTickets = $result->countOpen;
$pending = $result->countPending;
$unAssigned = $result->countUnassigned;
$unResolved = $result->countUnresolved;

How to implement IF Statement for more than two Parameters in Eloquent

In my Laravel 5.8 project, I have this query:
$approval_record = HrEmployee::selectRaw('count(is_approved) as count,is_approved, if (is_approved = 1, "Awaiting", "Approved") as approval')->where('company_id', $userCompany)->where('hr_status', 0)->groupBy('is_approved')->get();
In my DB, is approved has 1, 2 and Any other number.
1 is Awaiting, 2 is Approved, then any other number?
How do I modify the IF statement in my code above to achieve this?
thank to this article
you can use case:
$approval_record = HrEmployee::selectRaw('count(is_approved) as count,is_approved,
(CASE COALESCE (is_approved, 0) WHEN "1" THEN "Awaiting" WHEN "2" THEN "Approved"
WHEN "0" THEN "empty" ELSE "somethingElse"
)as approval')
->where('company_id', $userCompany)->where('hr_status', 0)->groupBy('is_approved')->get();

Wrong total pagination with select distinct in Laravel 6

I use Laravel 6.12, I have this request :
$queryJob = DB::table('jobs as j')->join('job_translations as jt', 'j.id', 'jt.job_id')
->whereNull('j.deleted_at')
->whereNull('jt.deleted_at')
->select('j.id', 'j.short_name', 'j.status', DB::raw("case when j.short_name = '{$request->short_name}' then 0 else 1 end"))
->distinct();
$jobs = $queryJob->paginate($qtyItemsPerPage);
The results displays an error for the total :
The total = 3, but as you can see the data contains only 2 elements.
I read here that when using a distinct, I must be clear on which column the total must be calculated: distinct() with pagination() in laravel 5.2 not working
So I modified my query like that:
$jobs = $queryJob->paginate($qtyItemsPerPage, ['j.*']);
But without success, the total is still wrong.
Hoping that I don't misunderstand your DB and relations structure and purpose of your query perhaps this will avoid using distinct or groupBy altogether?
$shortname = $request->input('short_name');
$queryJob = Job::with('job_translations')->select('id','short_name',
'status', DB::raw("case when short_name = '" . $shortname . "'
then 0 else 1 end")
->paginate($qtyItemsPerPage);
Pagination can be easily manually added with skip and take in case you need to use groupBy
$queryJob->skip(($page - 1) * $qtyItemsPerPage)->take($qtyItemsPerPage)->get();
The solution for me was to pass a field name to the distinct() method.
With your example:
$queryJob = DB::table('jobs as j')
// joins, where and other chained methods go here
->distinct('j.id')
Solution taken from https://stackoverflow.com/a/69073801/3503615

Laravel - using sum() with groupBy() does not return groupBy column with null value

I am trying to get sum of amounts group by time_ids. I am passing the following time_ids as an array.
Eloquent query is as follows
$time_ids = [1, 2, 3, 4];
$amount = Data::selectRaw('ifnull(sum(amount),0) as amount')
->whereIn('time_ids', $time_ids)
->groupBy('time_ids')
->pluck('amount')
->toArray();
As a result I get an array of only 3 values.
In my database, time_ids = 1 will not have any value, but I want the query to return 0 if it could not find any value.
Not enough reputation so adding it as an answer. Your query looks fine to me. Try querying the database directly and see what it is returning. Use this to generate the raw sql
$amount = Data::selectRaw('ifnull(sum(amount),0) as amount')->whereIn('time_ids',$time_ids)->groupBy('time_ids')->toSql();

why is this not possible in doctrine

I have to do some queries, while i was trying different ways, i found out that the next lines are not "recognized" by doctrine (it gives errors):
for example, when i want to compare if some data in the db is equal to a literal, here the condition:
('u.gender = M')
this is how my table look like:
id gender
1 M
2 M
3 F
it throws a semantical error. Also when comparing dates that way.
I would like to know why this is not recognized by doctrine, while comparing directly with numbers is accepted:
condition: ('u.age = 15')
First option you can do this way-
$M = 'M';
$age = 15;
$query = $this->createQueryBuilder('t')
->where('u.gender = :M AND u.age = :age')
->setParameters(array('M'=> $M,'age'=>$age);
another way to do this-
$query = $this->createQueryBuilder("t")
->where("u.gender = 'M' AND u.age = 15");
So i guess the answer to my question would be that it was not working because doctrine didn't recognize M as a string. That is why it was necessary to use inverted commas like #Mehedi said.
Another way of solving this was to use the query builder:
$v = 'M';
$condition = $this->qb->expr()->eq('u.gender', $this->qb->expr()->literal($v));
but i guess that is just long and hard to read. So the shortest thing would be just:
$condition = ("u.gender = 'M'");

Resources