A tutorial on database query in Laravel

Source: Internet
Author: User

The DB class in the Laravel framework allows us to easily perform database operations, such as common query queries:

Db::query (' SELECT * from users ');

Larvel also provides a fluent Query builder:db::table (' user ')->where (' id ', ' = ', ' 1 ')->get () similar to the active record in the CI framework;

Although there is little difference between the operation and the normal query, it is important to note that the Laravel query results are different from the native queries.

Simple set up a contents table test, there is a content field, we query:

$content = db::table (' contents ')->where (' id ', ' = ', ' 1 ')->get ();

Finally, print out the $content variable to see what the difference is:

Array (1) 
{
    [0]=> object (StdClass) #31 (1)  {
        ["content"]=> string (24) "This is a test ~ ~"} 
}

From above you can tell that the result of a query is an array of objects, so we must iterate before we get the content value:

foreach ($contents as $content)
{echo $content->content;
}

In general, Laravel database operations are easy to get started with.

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.