Home > other >  Check a specific Value that returned from eloquent in Laravel 8
Check a specific Value that returned from eloquent in Laravel 8

Time:09-29

In my Laravel Controller, I have the following code:

public function test($id)
{
    $emp_check = logentry::where([
        ['EmployeeFK', '=', $id],
        ['DateTime', '>', Carbon::now()->subHours(8)]
    ])->latest()->get();
    return $emp_check;
}

and these is the returning from database

{
"LogID": 2,
"DateTime": "2021-09-28 08:16:44",
"LocationX": null,
"LocationY": null,
"EmployeeFK": 1,
"Status": "checked out",
"ErrorResponse": null,
"SalaryChingValue": null,
"SalaryNote": null,
"LogNote": null,
"created_at": "2021-09-28 11:16:44"
}

is there any way to check the "status" value that returned from the eloquent result ?
I know there's a some specific way to do that but I really don't know how to write it.

CodePudding user response:

You can simply do the following before you return it:

if($emp_check['Status'] == 'your condition'){
   //do something
}

Not sure if that's what you need, if not, please update your question.

CodePudding user response:

maybe i misunderstood, but try

$emp_check = logentry::where('EmployeeFK', $id)
    ->where('DateTime', '>', Carbon::now()->subHours(8))
    ->where('Status', $status)
    ->latest()->get();

Or for one of statuses change to

$emp_check = logentry::where('EmployeeFK', $id)
    ->where('DateTime', '>', Carbon::now()->subHours(8))
    ->whereIn('Status', $statuses)
    ->latest()->get();

CodePudding user response:

As you are using get() method to find the result. The get() method will return an array of records that you can loop over. You are using id in where condition so if you always get a single record you can use:

if($emp_check[0]->Status == 'your status'){
    //do something
}

It's better to use first() to get single record. or you can use foreach loop

if($emp_check){
    foreach($emp_check as $v){
        if($v->Status == 'your status'){
            //do something
        }
    }
}
  • Related