orderBy('ticket_status', 'ASC') should not give you all new tickets.. did you even try this? it will give you tickets ordered by status -
Retrieve model with where conditions
Hi everyone,
I'm writing what is essentially a support ticket system. At the moment I retrieve tickets similar to this:
$tickets = Ticket::where('user_id', \Auth::user()->id)->orderBy('created_at', 'DESC')->get();
So this gets me all tickets that were created by the currently logged in user. Now each ticket has a ticket_status_id column which is an integer, similar to the following:
10 - new tickets 11 - re-opened tickets 20 - awaiting customer response 99 - closed
What I would like to do, rather than just sorting by the date the ticket was created is to group them by ticket status first, so anything that isn't ticket_status_id 99 appears first thus moving all closed tickets to the end of the list.
I don't think I simply want to orderBy('ticket_status', 'ASC') as this will give me all the new tickets, then the others, what i want is to essentially treat anything that isn't 99 as open (thus the same and order by created_at) and anything that is 99 as closed.
I think this is the query I want to run in terms of MySQL:
SELECT if (ticket_status_id = 99, 1, 0) as is_closed, tickets.* FROMticketsORDER BY is_closed ASC, created_at DESC
However, I'm not sure how I can manipulate a model to run this query for me and return a collection as objects as expected?
I hope that makes sense, any help is greatly appreciated.
I am not sure how 'fast/efficient' this will be but you could do:
// get all tickets
// then:
$tickets->groupBy(function ($item, $key) {
return $item->status !== 99;
});
then you will get two groups..
Please or to participate in this conversation.