Store data into database
Store data into database in laravel?
Here we will learn how to store data into database in Different Ways in Laravel. If you are new on laravel for store in database don’t worry here we will learn many different type of method to store data. Like store data throw Model property, query builder and upserts.
The “upsert” method to add new records to the database if they do not already exist, and to modify existing records with specified data. Its initial parameter contains the values meant for insertion or updating. The second parameter enumerates the column or columns that uniquely distinguish records within the related table. Finally, the method’s third and last parameter comprises an array of columns that are to be updated if a corresponding record already exists in the database. Further elucidation on this will follow below.
1. Model
In this method, we initiate a new object belonging to a specific model, then assign values to its respective columns before ultimately saving the object using the save() function. Alternatively, we can store data using the “inject attributes” method as demonstrated below.
public function store(Request $request)
{
$user = new User();
$user->name = $request->name;
$user->address = $request->address;
$user->email = $request->email;
$user->is_active = $request->is_active;
$user->save();
}
//-- ---- Inject attributes method ----- //
$user = new User([
'name' => $request->name,
'address' => $request->address,
'email' => $request->email,
'is_active' => $request->is_active
]);
$user->save();
2. Model Create And Model Create $request->all() method
We utilize the Eloquent Model for data insertion, allowing specification of column names/attributes for data insertion. Ensure that all fields in your $fillable property within your Model are included in the $request->all() function to facilitate this process.
protected $fillable = [‘name’, ‘address’, ’email’];
public function store(Request $request)
{
Product::create([
'name' => $request->name,
'address' => $request->address,
'email' => $request->email,
'is_active' => $request->is_active
]);
}
//------ OR ------ //
public function store(Request $request)
{
Product::create($request->all());
}
Model create method create a new object of that model and save data.
3. Model With Fill() and save() Method
In the fill() method to build/add all the fields with data on Eloquent model and save model object. Bellow the example.
public function store(Request $request)
{
$user = new User();
$user->fill([
'name' => $request->name,
'email' => $request->email,
'address' => $request->address,
'is_active' => $request->is_active
]);
$user->save();
}
4. Model using insert() method.
This insert() method uses the Query builder method and it do not creates or save timestamp fields in database by default instead it keeps it NULL. but in create() method it save with timestamp.
public function store(Request $request)
{
$user = [
'name' => $request->name,
'email' => $request->email,
'address' => $request->address,
'is_active' => $request->is_active
];
User::insert($user);
}
5. Model with firstOrCreate() method
The firstOrCreate
method is very similar to the firstOrNew
method. It tries to find a model matching the attributes you pass in the first parameter. If a model is not found, it automatically creates and saves a new Model after applying any attributes passed in the second parameter:
$product = Product::firstOrCreate(
[ 'name' => $request->name ],
[
'email' => $request->email,
]
);
Note — No call to $user->save() needed
6. Model with firstOrNew() method
The firstOrNew
method is really useful for finding the first Model that matches some constraints or making a new one if there isn’t one that matches those constraints.
$user = User::firstOrNew(['email' => request('email')]);
$user->name = request('name');
$user->save()
7. Model with updateOrCreate() method
The updateOrCreate method endeavors to locate a Model that satisfies the constraints provided in the first parameter. If a matching Model is located, it will be updated with the attributes specified in the second parameter. Conversely, if no matching Model is found, a new Model will be created incorporating both the constraints from the first parameter and the attributes from the second parameter.
$user = User::updateOrCreate(
['email' => request('email')],
['name' => request('name')]
);
// Do other things with the User
8. Query Builder
public function store(Request $request)
{
DB::table('users')->insert([
'name' => $request->name,
'email' => $request->email,
'address' => $request->address,
'is_active' => $request->is_active
]);
}