1, in-depth understanding Laravel eloquent (a)--basic concepts and usage

Source: Internet
Author: User

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.

  • Related Article

    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.