Answer the question
In order to leave comments, you need to log in
Why is looping with data from a database in Laravel not working correctly?
Here is the controller:
$tickets = Ticket::where('slot_id', $slot_id)->get();
$ar = [];
$slots = Slot::find($slot_id);
$i = 2;
do {
foreach (json_decode($tickets) as $ticket) {
if($ticket->num_ticket == $i) {
$ar[$i] = $ticket;
} else {
$ar[$i] = [];
}
}
$i++;
} while($i <= $slots->max_tickets);
return $ar;
{"2":[],"3":[],"4":[],"5":[],"6":{"id":3,"user_id":1,"slot_id":2,"num_ticket":6,"created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"},"7":[],"8":[],"9":[],"10":[]}
Answer the question
In order to leave comments, you need to log in
$tickets = Ticket::where('slot_id', $slot_id)->get();
$ar = [];
$slots = Slot::find($slot_id);
$i = 2;
do {
foreach (json_decode($tickets) as $ticket) {
if($ticket->num_ticket == $i) {
$ar[$i] = $ticket;
}
}
$i++;
} while($i <= $slots->max_tickets);
return $ar;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question