1. in-depth understanding of LaravelEloquent (1)-Basic concepts and usage

Source: Internet
Author: User
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.

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.