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