Insert Data
To Connectivity with Database

we put database details in .env file that is available in root folder. we can also put database detail in
config->database.php. But best way is that please enter detail in .env

DB_HOST = localhost
DB_DATABASE = homestead
DB_USERNAME = homestead
DB_PASSWORD = secret

How to insert database record
There are two way for work
1- By Sql Query
2- By Eloquent Model

By SQL Query

Route
Route::get('insert','InsertData@insertform');
Route::post('submit','InsertData@insert');

Form
<html>
<head>
<title>User Input Form</title>
</head>
<body>
<form name="frm" action="submit" method="post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>">
<table>
<tr>
<td>Name</td>
<td><input type = "text" name = "name" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type = "text" name = "email" /></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type = "submit" value = "Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>

Controller
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class InsertData extends Controller {
public function insertform(){
return view('user_form');
}

public function insert(Request $request){
$name = $request->input('name');
$email = $request->input('email');
DB::insert('insert into student (name,email) values(?,?)',[$name,$email]);
echo "Record inserted successfully.<br/>";
}
}

By Eloquent Model

Routes
Route::get('insert','InsertData@insertform');
Route::post('submit','InsertData@insert');

Form
<html>
<head>
<title>User Input Form</title>
</head>
<body>
<form name="frm" action="submit" method="post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>">
<table>
<tr>
<td>Name</td>
<td><input type = "text" name = "name" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type = "text" name = "email" /></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type = "submit" value = "Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>

Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
protected $table = 'student';
protected $fillable = [
'name', 'email', 'created_at','updated_at'
];

}

Controller
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Student;

class InsertDataModel extends Controller {
public function insertform(){
return view('user_form');
}

public function insert(Request $request){
$model=new Student();
$model->name = $request->input('name');
$model->email = $request->input('email');
$model->save();

}
}

Note : Two Coloumn created_at and updated_at is mandatory by insert data by model, In Laravel there is no folder for Model and Model is not necessary in Laravel, So for best practice we create Model folder in any location. In the Above code we created Model folder inside App Folder


Previous
Next