laravel 5.4 save data to database

Created at 31-Aug-2021 , By samar

laravel 5.4 save data to database

With this article, we’ll look at some examples of how to address the "laravel 5.4 save data to database" problem.

You can save data to database in laravel 5.4 using create() method and insert() method on query builder.
  • 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' => 'kayla@example.com',
        '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":"kayla@example.com","updated_at":"2021-08-31T08:24:43.000000Z","created_at":"2021-08-31T08:24:43.000000Z","id":16}
    

    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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.