1. in-depth understanding of LaravelEloquent (1) -- Basic concepts and usage: find the article with id 2 and print its title
$article = Article::find(2);echo $article->title;
Search for the article titled "I am the title" and print the id
$ Article = Article: where ('title', 'I am title')-> first (); echo $ article-> id;
Query all articles and print all titles cyclically
$ Articles = Article: all (); // The $ articles obtained here is a collection of objects. you can add '-> toArray ()' to the end of the object to change it to a multi-dimensional array. Foreach ($ articles as $ article) {echo $ article-> title ;}
The search id ranges from 10 to 10 ~ All articles between 20 and print all titles
$articles = Article::where('id', '>', 10)->where('id', '<', 20)->get(); foreach ($articles as $article) { echo $article->title; }
Query all articles and print all titles cyclically. Sort by updated_at in reverse order.
$articles = Article::where('id', '>', 10)->where('id', '<', 20)->orderBy('updated_at', 'desc')->get(); foreach ($articles as $article) { echo $article->title; }
Basic usage points
1. each class that inherits Eloquent has two 'fixed use' Article: find ($ number) ''' Article: all ()', the former will get an object with the value obtained from the database, and the latter will get a collection of objects containing the entire database.
2. all intermediate methods, such as 'Where () ''' orderBy () ', support both 'static' and 'non-static chains', that is, 'Article :: where ()... 'and 'article ::.... -> where ()'.
3. all 'unfixed use' calls require an operation to 'finalize'. There are two 'final operation' in this tutorial: '-> get () 'and'-> first ()'.
4. if you do not understand why many methods such as '-> where () ''-> get ()' can be used in the 'article' class, you need to read the document on object inheritance in PHP: object inheritance.