Phenomenon :
When querying SQL, the lists function is used to get a collection of the specified columns, but an abnormal error occurs, as follows
The query statement is:
Class Dateattrmodel extends basemodel{...
static function GetDays (...) {
$days = self::lists (' date ');
....... return $days;
}
if (In_array ($date, Dateattrmodel::getdays (...)) { //Here is an error, see the meaning is In_array the second parameter should be an array but passed an object
...
}
reason :
There are two Builder classes in laravel that have member functions lists: Query Builder (base class is Illuminate\database\query\builder) and eloquent (illuminate\database\ Eloquent\builder). The lists functions of the query constructor in Laravel 4.2 and 5.1 return array, but eloquent lists in Laravel 5.1 Returns the collection object type, and Laravel The eloquent lists in 4.2 returns the array type. The Basemodel here is inherited from the Illuminate\database\eloquent\model class, so the above call lists returns the collection object rather than the array type, so it is wrong to operate as an array in use.
Solution Method :
The correct use is to convert the Colletion object to array type and collection object to an array using the function ToArray ():
$days = self::lists (' date ')->toarray ();