How To Insert Data Into Table Using Model Name In Laravel

admin_img Posted By Bajarangi soft , Posted On 06-09-2020

INSERT INTO statement is used to insert new records in a table in Sql but in laravel To create a new record in the database, create a new model instance, set attributes on the model.

How To Insert Data Into Table Using Model Name In Laravel

Inserts
Once you have created a model and its associated database table, Now we start to add new data into table using model name. 
To learn more about creating model visit our Bajarangisoft site.


Example(1)
We have demo.php model using this model we can add new data into table

<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class demo extends Model
{
    use SoftDeletes;

    public $table = 'demos';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'desc'
    ];

   
    protected $casts = [
        'id' => 'integer',
        'name' => 'string',
        'desc' => 'string'
    ];

   
    public static $rules = [

    ];
}
 

In this demo model we connected  to demos table and we have different coulmn names with datatype.

Now we create form in demo.blade.php 

<!DOCTYPE html>
<html>
<head>
    <title>Insert data using model in laravel </title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>Insert Data Into Table Using Model Name In Laravel </h1>
    </div>
    <br>
    <br>
    <br>
    <h2></h2>
    <div class="form-group col-sm-2"></div>
    <div class="form-group col-sm-8">
    <div class="well">
        <form action="" method = "post">
            <!-- Name Field -->
                <label>Name</label>
                <input type="text" name="name" class="form-control">
                <br>
                <label>Desc</label>
                <input type="text" name="desc" class="form-control">
                <br>
                <input type="submit"  class="btn btn-primary">
        </form>
        <div class="form-group col-sm-2"></div>
     </div>
    </div>
    <br>
</div>
</body>
</html>


When data passed from demo.blade.php file  form calls  index.store function using route 

Route::post('demos', 'demoController@store');


Now we can see below code of democontroller in that we have store function which store data comes from demo.blade.php
 
<?php
namespace App\Http\Controllers;
use App\Models\demo;
use Illuminate\Http\Request;

class demoController extends AppBaseController
{
    public function store(Request $request)
    {
        $demo = new demo;
        $demo->name = $request->name;
        $demo->desc = $request->desc;
        $demo->save();
        Flash::success('Demo saved successfully.');
        return redirect(route('demos.index'));
    }
}

In this example, we assign the name and desc parameter from the incoming HTTP request to the name attribute of the App\demo model instance. When we call the save method, a record will be inserted into the database. The created_at  and updated_at timestamps will automatically be set when the save method is called, so there is no need to set them manually.

Related Post