MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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:-

Laravel migration creating products table with default values for name, quantity and description

Method 1: Through object.

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

Where, $project = Object.

See Output below:-

PhpMyAdmin products table with a new row added through a model object, id 1 Sushant Kumar

Method 2: Through Controller

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

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.

PhpMyAdmin row inserted using fillable, quantity stored as default 0 because it was not fillable

insertion by using guarded.

PhpMyAdmin row inserted using guarded, name stored as Default_Name because name is guarded

method 3: Through create() method

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

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

See the output below:-

PhpMyAdmin products table with row id 6 added by the Laravel create() method

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

How to merge two or multiple tables to each other in the Laravel PHP Framework? (Part-2)

How to merge two or multiple tables to each other in the Laravel PHP Framework? Part-1 Part-3 Step 1. Go to app/Country.php. Now, we need to list…

Read More

CRUD Operation Using Model in Laravel.

Inserting data in Database through Model. (Click to Open) Read, Update, and Delete data through Model. (Click to Open) Sushant Kumar With MotoShare.in, you can book a…

Read More

Read, Update, and Delete data through Model.

Read Data from Database through Model First, we have to connect our model and database in our controller. For that we use below code in our controller:-…

Read More

How to Update and Delete Data From Database table using Tinker.

How to Update Data in the Database table. We use the update command to update the data in the database table. Syntax Suppose, I want to update…

Read More

Python Tutorials: Database operations using python – mysql

This tutorial connects Python to MySQL with the official mysql-connector-python driver and runs the full set of CRUD operations: create a database and table, then insert, read,…

Read More

What is Laravel and use cases of Laravel?

What is Laravel? Laravel is an open-source, free PHP web framework designed for the development of web applications following the model-view-controller (MVC) architectural pattern. It was generated…

Read More