Eloquent basic usage
Find an article with ID 2 to print its title
$article = Article::find (2); Echo $article->title;
Find the article titled "I am title" and print the ID
$article = Article::where (' title ', ' I am the title ')->first (); Echo $article->id;
Check out all the articles and cycle through all the headings
$articles = Article::all (); The $articles obtained here is a collection of objects, which can be appended with '->toarray () ' to become multidimensional arrays. foreach ($articles as $article) { echo $article->title;}
Find all the articles in 10~20 between IDs and print all titles
$articles = article::where (' id ', ' > ', ten)->where (' id ', ' < ', ')->get (); foreach ($articles as $article) { echo $article->title;}
Check out all the articles and cycle through all the headings, sorted in reverse order Updated_at
$articles = article::where (' id ', ' > ', ten)->where (' id ', ' < ', ')->orderby (' Updated_at ', ' desc ')->get (); foreach ($articles as $article) { echo $article->title;}
Basic use Points
1. Each class that inherits the eloquent has two ' fixed usage ' article::find () ' Article::all () ', which gets an object with the values taken out of the database, which gets a collection of objects containing the entire database.
2. All intermediate methods such as ' Where () ' () ' () ' are able to support both ' static ' and ' non-static chaining ' methods, i.e. ' article::where (... ') and ' Article::....->where () '.
3. All ' non-fixed usage ' calls end up with an action to ' close ', with two ' finishing touches ' in this tutorial: '->get () ' and '->first '.
4. If you do not understand why the ' article ' class can use many methods such as '->where (') '->get () ', you need to read the document inherited by the PHP object: Object inheritance.