🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Inserting data in Database through Model.

There are many methods for inserting data in database through Model. See below:-

Migration is common in all methods in which we defined the table name and default value of all column’s. See below the migration code:-

Method 1: Through object.

We can insert data using Object in our controller. See the below code for help:-

$product= new product();
$product->name="Sushant Kumar";
$product->quantity=12;
$product->description="This is description";
$data = $product->save();
print_r("ID is ");
print_r($product["id"]);

Where, $project = Object.

See Output below:-

Method 2: Through Controller

We can insert data by using constructor in our controller. See the below code for help:-

$product= new product([
"name"=>"Sushant Kumar",
"quantity"=>12,
"description"=>"This is description"]);
$data = $product->save();
print_r("ID is ");
print_r($product["id"]);

But, whenever we are inserting data by using controller then we need to use fillable or guarded property in model otherwise it returns an error.

See below how to use fillable:-

protected $fillable = [“key1″,”key2”];

Note:- We add value of key in fillable which we want to add in the database.

See below how to use guarded:-

protected $guarded = [“key1″,”key2”];

Note:- We add the value of key in guarded which we don’t want to add in the database.

See the Output below:-

insertion by using fillable.

insertion by using guarded.

method 3: Through create() method

We can insert data using create() method in our controller. See the below code for help:-

$product= product::create(["name"=>"Sushant Kumar","quantity"=>12,"description"=>"This is description"]);
$data = $product->save();
print_r("ID is ");
print_r($product["id"]);

As above property, it also depends on fillable and guarded.

See the output below:-

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.