Я использую Laravel 5, и я хочу сделать поиск массива. Но проблема в том, что запрос возвращает объект, так что я сделал это типаж его, когда я типаж его в массив еще есть объект в.Поиск объектов PHP Laravel 5
Так что я эту таблицу
FormResponses
->id
->form_id
->metrics_id
Тогда мой запрос
$responses = FormResponses::where('form_id', '>=', 1)->where('form_id', '<=', 500)->get();
$responses = (array) $responses;
$neededObjects = array_filter(
$responses,
function ($e) {
return $e->form_id == 1 && $e->metrics_id == 1;
}
);
Итак, как вы видите, $needObjects
моя переменная для отфильтрованного массива. Я делаю это для поиска записи. Вы можете увидеть
return $e->form_id == 1 && $e->metrics_id == 1;
означает, что я хочу, чтобы найти все записи в массиве, где
form_id = 1
и metrics_id = 1
То есть выход $responses
когда НЕ отливать в массив
object(Illuminate\Database\Eloquent\Collection)[3423]
protected 'items' =>
array (size=3072)
0 =>
object(App\Http\Models\FormResponses)[3424]
protected 'table' => string 'forms_responses' (length=15)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=7)
...
protected 'original' =>
array (size=7)
...
protected 'relations' =>
array (size=0)
...
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'appends' =>
array (size=0)
...
protected 'fillable' =>
array (size=0)
...
protected 'guarded' =>
array (size=1)
...
protected 'dates' =>
array (size=0)
...
protected 'dateFormat' => null
protected 'casts' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'with' =>
array (size=0)
...
protected 'morphClass' => null
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
после $responses = (array) $responses;
array (size=1)
'�*�items' =>
array (size=3072)
0 =>
object(App\Http\Models\FormResponses)[3424]
protected 'table' => string 'forms_responses' (length=15)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=7)
...
protected 'original' =>
array (size=7)
...
protected 'relations' =>
array (size=0)
...
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'appends' =>
array (size=0)
...
protected 'fillable' =>
array (size=0)
...
protected 'guarded' =>
array (size=1)
...
protected 'dates' =>
array (size=0)
...
protected 'dateFormat' => null
protected 'casts' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'with' =>
array (size=0)
...
protected 'morphClass' => null
public 'exists' => boolean true
public 'wasRecentlyCreated' => boolean false
Laravel имеет свой собственный метод, названный как '-> ToArray() '. Поэтому вместо typecasting вы можете просто использовать '$ response-> toArray()' –