Getting started with the database running the original SQL lookup

Source: Internet
Author: User

Getting started with the database Run the original SQL lookup

Once you've set up your database connection, you can use DB facade to find it. DB Facade provides a lookup method for each type:select,update,insert , Delete , Statement . the following brothers to help you introduce each.

Run a Select Find #

use select in DB facade to run a basic lookup:

<?php

namespace App\http\controllers;

Use DB;

Use App\http\controllers\controller;

Class Usercontroller extends Controller

{

/**

* displays a list of all users in the application.

*

* @return Response

*/

Public Function Index ()

{

$users = Db::select (' select * from users where active =? ', [1]);

Return view (' User.index ', [' users ' and ' = $users]);

}

}

The first parameter passed to The Select method is the original SQL Lookup, and the second parameter is the parameter binding that is required for any lookup. Typically, these are the qualified values of the WHERE statement. Parameter bindings are primarily intended to prevent SQL injection.

The Select method always returns the array data for the result. Each result in the array is a PHP StdClass object, which allows you to access the value of the result:

foreach ($users as $user) {

Echo $user->name;

}

using named Bindings #

in addition to using ? to represent your parameter bindings, you can also run a lookup using a named binding:

$results = Db::select (' select * from users WHERE id =: id ', [' id ' = 1]);

Run insert#

to run the insert syntax, you can use the Insert method in DB facade . As with select , the first parameter of this method is the original SQL Lookup, and the second argument is the binding:

Db::insert (' INSERT into users (ID, name) VALUES (?,?) ', [1, ' Dayle ']);

Run update#

The Update method is used to update records that already exist in the database. The method returns the number of rows affected by this declaration:

$affected = db::update (' update users set votes = + WHERE name =? ', [' John ']);

Run delete#

The Delete method is used to delete records that already exist in the database. As with update , the number of rows deleted will be returned:

$deleted = DB::d elete (' Delete from users ');

run a general declaration #

sometimes some database operations should not return any parameters. For this type of operation, you can use the statement method in DB facade:

Db::statement (' drop table users ');

Listen for Find events #

If you want to be able to monitor every piece of program execution SQL statement, you can use the Listen method. This method is useful for record lookups and debugging. You can register your lookup listeners in the service container:

<?php

namespace App\providers;

Use DB;

Use Illuminate\support\serviceprovider;

Class Appserviceprovider extends ServiceProvider

{

/**

* Start any application's services.

*

* @return void

*/

Public Function boot ()

{

Db::listen (function ($sql, $bindings, $time) {

//

});

}

/**

* Register a service provider.

*

* @return void

*/

Public Function Register ()

{

//

}

}


Getting started with the database running the original SQL lookup

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.