How to add only one data in database table in laravel?

Member

by samara , in category: PHP General , 2 months ago

How to add only one data in database table in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , 2 months ago

@samara 

To add only one data in a database table in Laravel, you can use the create method of the model class associated with the table.


Here are the steps to add a single data entry:

  1. Create a new instance of the model class, usually within a controller method. For example, if you have a User model associated with the users table, you can create a new user like this: use AppModelsUser; public function store(Request $request) { $user = new User; // ... }
  2. Assign the values you want to add to the respective model attributes. For example, if your users table has name and email columns, you can assign values like this: $user->name = $request->input('name'); $user->email = $request->input('email');
  3. Save the model to insert the data into the database using the save method: $user->save();


That's it! The above steps will add a new data entry into the specified database table using Laravel's Eloquent ORM.