Does anyone have an idea why I am getting this kind of array? I only want this below part. I need to remove those mysql connection and other unwanted arrays from this.
array (
'name' => 'Westwood',
'id' => 538,
),
0 =>
Common\Models\Property::__set_state(array(
'connection' => 'mysql',
'table' => NULL,
'primaryKey' => 'id',
'keyType' => 'int',
'incrementing' => true,
'with' =>
array (
),
'perPage' => 15,
'exists' => true,
'wasRecentlyCreated' => false,
'attributes' =>
array (
'name' => 'Westwood',
'id' => 538,
),
'guarded' =>
array (
0 => '*',
),
)),
The below code shows what I did to get that array. when I Log::info($results);
I get that array hope you understand my question.
$properties = model::where('status', '=', 'Active')
->get();
if($jsonData->city !== "") {
foreach ($properties as $property) {
if($property->city === $jsonData->city) {
$results[] = $property;
}
}
}
As you are using get()
it will return a laravel collection, not an array, if you want an array, you can use toArray()
it would look like this:
$properties = prop_model::where('status', '=', 'Active')
->where('propertylive', '=', 'Yes')
->get()
->toArray();
Answer:
Based on get() manual
The get method returns an Illuminate\Support\Collection containing the
results where each result is an instance of the PHP stdClass object.
You may access each column’s value by accessing the column as a
property of the object
it will return a laravel collection.
So use toArray() to get Array
The toArray method converts the collection into a plain PHP array. If
the collection’s values are Eloquent models, the models will also be
converted to arrays
Also specify filed names to narrow down your array as well:
$properties = prop_model::select('name','id')
->where(['status', '=', 'Active'],['propertylive', '=', 'Yes'])
->get()
->toArray();
Reference taken: How to Create Multiple Where Clause Query Using Laravel Eloquent?
Answer:
get()
function returns collection. So as per Laravel Documentation you can use toArray()
function
The
toArray
method converts the collection into a plain PHP array. If the collection’s values are Eloquent models, the models will also be converted to arrays.
$properties = prop_model::where('status', '=', 'Active')
->where('propertylive', '=', 'Yes')
->get()
->toArray();