Learn new Frame Laravel 5.6 (next day)-DB, controller and model use

Source: Internet
Author: User

DB class use, controller usage and model use

Link database:

/config/database.php

/.env

  

db_connection=mysqldb_host=127.0.0.1db_port=3306db_database=testdb_ USERNAME=Rootdb_password=root

Querying MySQL data in the controller

 Use Illuminate\support\facades\db;
//Get Data        $data= db::table (' Test ')->where (Array(' id ' = + 1)] ->get ();//TP in select ();Dump$data); $data= db::table (' Test ')->where (Array(' id ' = + 1)] ->first ();//TP in Find ()Dump$data); $data= db::table (' Test ')->where (Array(' id ' = + 1)] ->pluck (' name ');//TP in column ()Dump$data); $data= db::table (' Test ')->where (Array(' id ' = + 1)] ->select (' id ', ' name ')->get ();//tp in field ()Dump$data); ////Insert        $data= db::table (' Test ')->insert ([' name ' = ' = ' [email protected] ', ' sex ' = 0]);//returns the bool//TP in Add ()Dump$data); $id= db::table (' Test ')->insertgetid ([' name ' = ' = ' [email protected] ', ' sex ' = 0]);//return IDDump$id); //Update        $data= db::table (' Test ')          ->where (' id ', 1)          ->update ([' sex ' = 0]);//returns the number of update bars in//TP save ()Dump$data); DB:: Table (' Test ')->where ([' id ' = 1])->increment (' num ', 5);//self-increment//tp in Setinc ()Db::table (' Test ')->where ([' id ' = 2])->decrement (' num ', 5);//TP in Setdec ()//deleteDb::table (' Test ')->where ([' id ' = 1])Delete (); DB:: Table (' Test ')->truncate ();//TRUNCATE TABLE, clear table contents, self-increment restore to initial value 1//Database transaction processingDb::transaction (function(){            $id= db::table (' Test ')->insertgetid ([' name ' = ' = ' [email protected] ', ' sex ' = 0]);//return IDDb::table (' Test ')->where ([' id ' = =$id])->increment (' num ', 5);//Self-increment        }); DB::BeginTransaction (); DB::RollBack (); DB:: Commit ();

Learn new Frame Laravel 5.6 (next day)-DB, controller and model use

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.