Sami Sami - 7 months ago 32
PHP Question

How can I use array_where in Laravel?

I have an array (

) returned by an Eloquent query with the following JSON encoded output:

[{"id":1, "user_id":"34","amount":"1000","status":"0","created_at":"2016-08-18 14:24:59","updated_at":"2016-08-18 14:24:59"},
{"id":3, "user_id":"33","amount":"300","status":"1","created_at":"2016-08-18 14:31:04","updated_at":"2016-08-18 14:33:20"},
{"id":4, "user_id":"33","amount":"1000","status":"0","created_at":"2016-08-18 14:31:27","updated_at":"2016-08-18 14:31:27"},
{"id":5, "user_id":"34","amount":"400","status":"1","created_at":"2016-08-18 14:42:02","updated_at":"2016-08-18 14:42:02"}]

I want to use the
method in Laravel and filter
according this condition :
status == 1
, could anyone tell me how to do that?


If this is the result of an Eloquent query, it's not an array, it's a Collection. From the documentation:

All multi-result sets returned by Eloquent are an instance of the Illuminate\Database\Eloquent\Collection object

You can use the Collection's filter method, which takes a closure, and return the condition you want ('status' == 1) in the closure.

$filtered = $your_query_result->filter(function($value, $key) {
    return $value->status == 1;