laravel 5.4 save data to database

You can save data to database in laravel 5.4 using create() method and insert() method on query builder.

Answers 1
  • Save record using create method in Laravel

    //Create method to save record in users table
    //You can use code inside of controller's method
    $user = User::create([
        'name' => 'Kayla',
        'email' => '[email protected]',
        'password' => Hash::make('secret')
    ]);
    return $user;
    
    //Import user model after namespace in controller
    //Import user model in laravel 8
    use App\Models\User;
    
    //Import user model in laravel version < 8
    use App\User;
    
    //Output:
    {"name":"Kayla","email":"[email protected]","updated_at":"2021-08-31T08:24:43.000000Z","created_at":"2021-08-31T08:24:43.000000Z","id":16}
    
    0

    You can create record in table using create method. Create method returns the object of created record if query executed successfully.

  • Back to code snippet queries related laravel