3

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;
        }
    }
}
123t
  • 77
  • 1
  • 10

3 Answers3

3

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();
Gamopo
  • 1,600
  • 1
  • 14
  • 22
  • inside foreach i have array (`$filteredProperty[] = $property;`) and that array gives me the above array. i want to remove those mysql connection values from the array. do you know how to do it – 123t Mar 13 '20 at 11:14
  • 1
    As @Gamopo has explained in his anwers, you should use the `toArray()` method after calling `get()`. This converts the result from a collection object (as documented in the Laravel docs) to an associative array as you require. +1 OP – Peter B Mar 13 '20 at 11:32
2

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?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

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();

Laravel -> Collection -> toArray

Sehdev
  • 5,486
  • 3
  • 11
  • 34