In Laravel5.4, use the lists method to return a collection composed of specified fields. In this article, we will introduce the usage of the lists method and related considerations.
Usage:
In Laravel5.4, we can use the following method to call the lists method:
$collection = Model::pluck('column')->toArray();
This method will return a collection composed of the specified fields. Among them, 'column' is a certain column in the data table to be queried, which can be any column. The toArray() method is used to convert the result into an array.
For example, we want to get the IDs of all users from a data table named users. We can write it like this:
$userIds = User::pluck('id')->toArray();
In this way, we get an array containing all user IDs. .
Note:
Summary:
The lists method of Laravel5.4 can easily obtain a certain column of data in the specified data table, which improves our work efficiency. However, since this method has been abandoned, it is recommended to use the pluck method of Laravel 5.5 and above to obtain a certain column of data in the specified data table. During use, we must also pay attention to the naming of column names to avoid special characters such as 'null', '1' or '0'.
The above is the detailed content of How to use and pay attention to the lists method in laravel5.4. For more information, please follow other related articles on the PHP Chinese website!