Posted inLaravel / Mysql / Mysqli / php

How to insert data in database using Laravel framework

Today, We want to share with you insert form data into database using laravel.In this post we will show you insert form data into database using laravel 6, hear for laravel insert data into table without model we will give you demo and example for implement.In this post, we will learn about insert query in laravel with an example.

laravel insert data into table using model

There are the Following The simple About insert data in laravel 7 Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel insert query mysql, so the laravel db::query is used for this example is following below.

how to insert data in database using laravel framework PHP.

For insert data in MySQL using laravel first i have to make a table in data base.

here The INSERT INTO statement is used to insert create fresh new data to a MySQL table in data base:

INSERT INTO statement

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

To learn more about SQL, please visit our SQL tutorial.

For creating table the SQL query is:

Step 1: SQL Query

CREATE TABLE member_details


CREATE TABLE member_details
(
id int NOT NULL AUTO_INCREMENT,
profile_nm varchar(50),
real_nm varchar(50),
full_address varchar(50),
email varchar(50),
PRIMARY KEY (id)
);

Create 3 files for insert data in Laravel :

  • MemberInsertController.php (app/Http/Controllers/MemberInsertController.php)
  • Member_create.php (resources/views/Member_create.php)
  • web.php (routes/web.php)

Step 2: View File

Member_create.blade.php





member Management | Add


@if (session('status'))

@elseif(session('failed'))

@endif
First Name
Last Name
City Name
Email

Step 3: Controller File

MemberInsertController.php


 'required|string|min:3|max:255',
			'full_address' => 'required|string|min:3|max:255',
			'email' => 'required|string|email|max:255'
		];
		$validator = Validator::make($request->all(),$rules);
		if ($validator->fails()) {
			return redirect('insert')
			->withInput()
			->withErrors($validator);
		}
		else{
            $data = $request->input();
			try{
				$member = new MemberInsert;
                $member->profile_nm = $data['profile_nm'];
                $member->real_nm = $data['real_nm'];
				$member->full_address = $data['full_address'];
				$member->email = $data['email'];
				$member->save();
				return redirect('insert')->with('status',"Insert successfully");
			}
			catch(Exception $e){
				return redirect('insert')->with('failed',"operation failed");
			}
		}
    }
}

Step 4: Model File

MemberInsert.php



Step 5: Routes File

web.php



How To Insert New Database Records in Laravel Eloquent?

To insert a new record into a database using Laravel's Eloquent ORM, you can follow these steps:

Create a new instance of the model you want to insert a record for.

$newRecord = new YourModel();

Set the values of the attributes for the new record.

$newRecord->attribute1 = 'value1';
$newRecord->attribute2 = 'value2';

Alternatively, you can pass an array of attribute values to the model's constructor:

$newRecord = new YourModel([
    'attribute1' => 'value1',
    'attribute2' => 'value2',
]);

Call the save() method on the new record instance.

$newRecord->save();

This will insert a new record into the database with the attribute values you have set.

Alternatively, you can use the create() method to create and save a new record in one step:

YourModel::create([
    'attribute1' => 'value1',
    'attribute2' => 'value2',
]);

Note that when using the create() method, all the attributes in the model's $fillable property (or $guarded property if you want to specify which attributes are not mass-assignable) must be set in the array passed to create().

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about insert form data into database using laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype