Home > Back-end >  Automatically not insert created_at and updated_at in laravel not working
Automatically not insert created_at and updated_at in laravel not working

Time:02-14

Automatically not insert created_at and updated_at in laravel not working.

DB::table('commundityvendordata')->insert($commdata);

I am using this above statement passing an array for inserting multiple record $commdataworking fine but when check my created_at and updated_at column in database not getting timestamp.

Model:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class commundityvendordata extends Model
{
    use HasFactory;
    protected $table ='commundityvendordata';
}

Migration:

public function up()
    {
        Schema::create('commundityvendordata', function (Blueprint $table) {
            $table->bigIncrements('vender_id')->unsignedBigInteger();
            $table->date('new_date');
            $table->unsignedBigInteger('cd_id');
            $table->foreign('cd_id')->references('cd_id')->on('communitydata')->onDelete('cascade');
            $table->unsignedBigInteger('cn_id');
            $table->foreign('cn_id')->references('cd_id')->on('communitydata')->onDelete('cascade');
            $table->unsignedBigInteger('unit_id');
            $table->foreign('unit_id')->references('unit_id')->on('units')->onDelete('cascade');
            $table->unsignedInteger('vender1');
            $table->unsignedInteger('vender2');
            $table->unsignedInteger('vender3');
            $table->unsignedInteger('vender4');
            $table->timestamps();
        });
    }

Controller:

function commundityvendordata(Request $request){
                $collection = count(collect($request));
                $cvendordata = new commundityvendordata;
                for ($i=0; $i < $collection; $i  ) {
                $new_date = Carbon::parse($request->new_date)->format('Y-m-d');
                $CCode=communitydata::where('c_code','=',$request->ccode[$i])->first();
                $cd_id = $CCode['cd_id'];
                $CName=communitydata::where('c_name','=',$request->cname[$i])->first();
                $cn_id = $CName['cd_id'];
                $CUnit=units::where('unit','=',$request->cunit[$i])->first();
                $unit_id = $CUnit['unit_id'];
                $vender1 = $request->vendor1[$i];
                $vender2 = $request->vendor2[$i];
                $vender3 = $request->vendor3[$i];
                $vender4 = $request->vendor4[$i];
                   $commdata = [
                    'new_date'  => $new_date,
                    'cd_id'     => $cd_id,
                    'cn_id'     => $cn_id,
                    'unit_id'   => $unit_id,
                    'vender1'   => $vender1,
                    'vender2'   => $vender2,
                    'vender3'   => $vender3,
                    'vender4'   => $vender4
                ];
                if($cd_id != ''){
                         DB::table('commundityvendordata')->insert($commdata);
                    }else{
                        return back()->with('success','Data Saved...');
                    } 
            }   
    }

database table screen shot: enter image description here

CodePudding user response:

created_at and updated_at are only automatically inserted if you are using Eloquent.

If you are using the DB facade you will have to manually insert them:

      $commdata = [
                    'new_date'  => $new_date,
                    'cd_id'     => $cd_id,
                    'cn_id'     => $cn_id,
                    'unit_id'   => $unit_id,
                    'vender1'   => $vender1,
                    'vender2'   => $vender2,
                    'vender3'   => $vender3,
                    'vender4'   => $vender4,
                    "created_at" =>  \Carbon\Carbon::now(), # new \Datetime()
                    "updated_at" => \Carbon\Carbon::now(),  # new \Datetime()
                ];

If you want to use Laravel at the fullest and don't have advanced/complex queries, I recommend to you using Models and Eloquent.

CodePudding user response:

This is because you haven't set the default value for 'created_at' and 'updated_at' For doing so you can follow 2-3 solutions:

Solution 1:

  1. Go to phpmyadmin
  2. Select Database table. (in your case its commundityvendordata)
  3. Then go to structure and set the default value for created_at and updated_at
  4. And for updated_at you can set the Attribute to on update Current_Timestamp

Solution 2:
In your commdata array just add the created_at and updated_at

$commdata = [
...
'created_at' => date("Y-m-d h:i:s"),
'updated_at' => date("Y-m-d h:i:s"),
];

Solution 3:
Is to make changes in your Scheme

Schema::create('commundityvendordata', function (Blueprint $table) {
$table->timestamps('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
});
}

  • Related