hendraspt
hendraspt

Reputation: 969

My select box give me the wrong value. How to do foreach in laravel?

I am using laravel 5.2 and I have a select box in my form. The select box is for a list of the available driver at that time. I only have 2 drivers and all the driver are booked. But when I make a driver reservation, there is 1 driver show on the list. I think I have a mistakes in my foreach. Am I right? Do you know how to fix that? This below is my controller code:

    $driverReserved = Reservation::select("RES_DRIVER")->where("RES_RETURN", '>', $reservation[0]->RES_DEPARTURE)->where('RES_STATUS', '=', 'Assigned')->Where('RES_SPK', '!=', $request['RES_SPK'])->exists();

    $driverReserved1 = Reservation::select("RES_DRIVER")->where("RES_RETURN", '>', $reservation[0]->RES_DEPARTURE)->where('RES_STATUS', '=', 'Assigned')->Where('RES_SPK', '!=', $request['RES_SPK'])->distinct()->get();

    if(empty($driverReserved)){
        $driver = vDriver::all();
    }
    else{
        foreach($driverReserved1 as $item1) {
            $driver = vDriver::where("REF_TEXT", "!=", $item1['RES_DRIVER'])->get();
        }

    }

And this is when I do the print_r($dirverReserved1);die; . There is 2 driver in it.

Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Reservation Object ( [timestamps] => [primaryKey:protected] => RES_ID [table:protected] => RESERVATION [connection:protected] => [perPage:protected] => 15 [incrementing] => 1 [attributes:protected] => Array ( [RES_DRIVER] => Asim ) [original:protected] => Array ( [RES_DRIVER] => Asim ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [dateFormat:protected] => [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] => ) [1] => App\Reservation Object ( [timestamps] => [primaryKey:protected] => RES_ID [table:protected] => RESERVATION [connection:protected] => [perPage:protected] => 15 [incrementing] => 1 [attributes:protected] => Array ( [RES_DRIVER] => Sulhi Mukhlas ) [original:protected] => Array ( [RES_DRIVER] => Sulhi Mukhlas ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [dateFormat:protected] => [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] => ) ) )

But when I do print_r($item1['RES_DRIVER']);die; inside the foreach, it's only show 1 driver named Asim.

Upvotes: 1

Views: 51

Answers (2)

Sagar Gautam
Sagar Gautam

Reputation: 9389

I think you are using name of the driver for handling all these things but if you have used ID then it would better.

Anyway,

I think you should use pluck and whereNotIn in your code to get out of the problem like this,

Change your query for $driverReserved1 as,

$driverReserved1 = Reservation::select("RES_DRIVER")->where("RES_RETURN", '>', $reservation[0]->RES_DEPARTURE)->where('RES_STATUS', '=', 'Assigned')->Where('RES_SPK', '!=', $request['RES_SPK'])->distinct()->pluck('RES_DRIVER');

This will give you array of distinct driver name which are reserved. Now, using whereNotIn you can get drivers other than booked driver like this,

if(empty($driverReserved)){
    $driver = vDriver::all();
}
else{
        $driver = vDriver::whereNotIn("REF_TEXT",$driverReserved1)->get();
}

I think this will work for your case.

I hope you understand.

Upvotes: 1

Ahmed Shams
Ahmed Shams

Reputation: 358

First Check Laravel Collection it has pretty good functions

Second you the code is right and the output is right too :D

you just expected wrong output

    foreach($driverReserved1 as $item1) {
        $driver = vDriver::where("REF_TEXT", "!=", $item1['RES_DRIVER'])->get();
    }

here you just said last record will be saved in $driver so you need them all in driver you may use

array_merge($firstArray,$secondArray);

in your example here it will be like

foreach($driverReserved1 as $item1) {
        $driver = array_merge($driver ,vDriver::where("REF_TEXT", "!=", $item1['RES_DRIVER'])->get());
    }

and of course assigning driver before

Upvotes: 0

Related Questions