Use laravel framework orm instantiated model-save () method

1. First, create a database of information

CREATE TABLE `user` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `username` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  `password` varchar(999) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=63 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

2. Create route

Route::get('demo','DemoController@demo');

3. Create a model

<? PHP 

namespace the App \ Models; 

use Illuminate \ Database \ Eloquent \ the Model; 

class the User the extends the Model 
{ 
    //
     protected  $ Table = 'User' ; // name correspondence table
    protected  $ Fillable = [ 'ID', 'username', 'password' ]; // create a database field corresponding to the
    public  $ timestamps = to false ; // disable timestamp 
}

 

4. Create a controller (plus save method)

<? PHP 

namespace the App \ the Http \ the Controllers; 

use the App \ Models \ the User; 

class DemoController the extends the Controller 
{ 
    //
     public  function Demo () 
    { 
        $ Data = new new the User (); // instantiation model
         $ Data -> username = ' Hu Ge ' ; // name assigned to the field
         $ Data -> password = MD5 (' 222 ' ); // password field assignment to
         $ Data -> save (); // save the information 
    } 
}

5. You will find your database in more than one piece of information

6. Let's look at another simple usage

6.1 update

 

Data $ = the User :: Find ( '. 3'); // direct inquiries ID 
        $ Data -> username = 'SS' ;
         $ Data -> password = MD5 ( '222' );
         $ Data -> Save (); / / use the save () method automatically maintain

 

Guess you like

Origin www.cnblogs.com/yaoliuyang/p/12538988.html