0

I'm trying to loop through a a string and get array of records where the service status is equals to complete or cancelled. I'm having an issue of get all the records instead it response with the last record from the list.

My Controller:

        $id = $request->user_id;
        $history = bookings::select('*')->whereIn('service_status', ['complete', 'cancelled'])->where(['specialist_id'=>$id])->orderBy('time', 'DESC')->get();

        if($history->isempty()){
            return response()->json(['statusCode'=>'5', 'statusMessage' => 'Empty Record', 'data' =>[]]);
        }else{
            for($i = 0; $i < count($history); $i++){
                $b_id = $history[$i]->id;
                $service = $history[$i]->service;
                $time = $history[$i]->time;
                $date = $history[$i]->date;
                $location = $history[$i]->location;
                $payment = $history[$i]->payment_type;
                $price = $history[$i]->amount;
                $special_request = $history[$i]->special_request;
                $s_id = $history[$i]->costumer_id;
            }
            $user = User::with('profile')->find($s_id);

            $data = array(['id'=>$b_id, 'date'=>$date, 'name'=>$user->name." ".$user->profile->lastname, 'speccial_request'=>$special_request, 'item'=>$service, 'time'=>$time." - 1 hour", 'location'=>$location, 'total'=>"$price"]);

            return response()->json(['statusCode'=>'0', 'statusMessage' => 'Successful','data' => $data], 200);
        }

My response is as follows:

{
    "statusCode": "0",
    "statusMessage": "Successful",
    "data": [
        {
            "id": 5,
            "date": "2020-07-08",
            "name": "User1",
            "speccial_request": null,
            "item": "Service",
            "time": "10:00 - 1 hour",
            "location": "street, city",
            "total": "300"
        }
    ]
}

I want to get all the items from the DB not just one.

2
  • Just out of curiousity, is there a reason your aren't using Eloquent queries or Collections? They are one of the most powerful aspects of the Laravel framework and would make this task much more straight forward! Commented Jul 8, 2020 at 11:51
  • the name that i want to display belongs to another user. trying to create a history function Commented Jul 8, 2020 at 11:57

1 Answer 1

1

You need to create an empty array ($data) and push to that array in each iteration. Try below code:

$id = $request->user_id;
$history = bookings::select('*')->whereIn('service_status', ['complete', 'cancelled'])->where(['specialist_id'=>$id])->orderBy('time', 'DESC')->get();

if($history->isempty()){
    return response()->json(['statusCode'=>'5', 'statusMessage' => 'Empty Record', 'data' =>[]]);
}else{
    $data=[];
    for($i = 0; $i < count($history); $i++){
        $b_id = $history[$i]->id;
        $service = $history[$i]->service;
        $time = $history[$i]->time;
        $date = $history[$i]->date;
        $location = $history[$i]->location;
        $payment = $history[$i]->payment_type;
        $price = $history[$i]->amount;
        $special_request = $history[$i]->special_request;
        $s_id = $history[$i]->costumer_id;
        $user = User::with('profile')->find($s_id);

        array_push($data,['id'=>$b_id, 'date'=>$date, 'name'=>$user->name." ".$user->profile->lastname, 'speccial_request'=>$special_request, 'item'=>$service, 'time'=>$time." - 1 hour", 'location'=>$location, 'total'=>"$price"]);
        }
    return response()->json(['statusCode'=>'0', 'statusMessage' => 'Successful','data' => $data], 200);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.