Home > Article > Backend Development > Common ways to get values from Laravel database
##find($id) Requires a primary key $id and returns a model object, if it does not exist, returns null
findOrFail($id) Requires a primary key $id and returns a model object. If it does not exist, an error occurs and an error is thrown
first() Returns the first record found in the database, if there is no matching data, returns null
firstOrFail() Returns the first record found in the database, if it does not exist An error occurs in the matching data and an error is thrown.
get() Returns the matching data model collection
pluck($column1,$column2,. ..) Only returns a collection of values for the specified field. In previous versions of Laravel, this method was called lists. [Commonly used]
value($column) Returns only a collection of values of the specified field
toArray() Convert the model/collection to Simple PHP array. [A: A set is a reinforced array. It functions similarly to an array, but with more capabilities. B: PHP does not allow collection objects to be used anywhere to use arrays. Newer versions of PHP have iterabletypehints that can be used to accept arrays and collections. 】
all() Obtain an ordinary array from a collection
chunk($groupsize,function($item){}) Points Block processing, please note that when there are filter conditions, only half of the data is updated during self-update. If the amount of data is large, it is necessary to repeatedly call the block update method multiple times, and finally perform a one-time update with the minimum amount of data
forPage($page, $count) Data pagination
paginate($page_size) Built-in pagination [Add url parameters through the appends() method, $items ->appends['args'=>val], the frontend displays $page = $items->links() ]
Document reference: https://laravel.com/docs/ 5.5/queriesDocument reference: https://blog.csdn.net/OneGoal/article/details/80334599For more PHP related knowledge, please visit
PHP Chinese website!
The above is the detailed content of Common ways to get values from Laravel database. For more information, please follow other related articles on the PHP Chinese website!