laravel5.1 on the bug____ function of the lists function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.