Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why created_at updated_at are null on insertion

Tags:

php

laravel

In my laravel project i need to add multiple records at a time an i also want to insert the created_at and updated_at as works in save() automatically.But not working in insert method.

for($i=0; $i<count($req->location_id); $i++)
                    {
                            $asset_arr[$i]['property_id'] = $this->id;
                            $asset_arr[$i]['location_id'] = $req->location_id[$i];
                            $asset_arr[$i]['model_id'] = $req->model_id[$i];
                            $asset_arr[$i]['appliance_owner'] = $req->appliance_owner[$i];
                            $asset_arr[$i]['current'] = $req->current[$i];
                            if($req->installdate[$i]!=''){
                                $asset_arr[$i]['installdate'] = date('Y-m-d',strtotime($req->installdate[$i]));
                            }
                            $asset_arr[$i]['comment'] = $req->comment[$i];
                    }
                    if(!empty($asset_arr)){

                            Asset::insert($asset_arr);
                    }
like image 535
Alfiza malek Avatar asked Mar 11 '23 02:03

Alfiza malek


1 Answers

insert() is not an Eloquent method, so you'll need to add timestamps manually. Do something like this:

$asset_arr[$i]['created_at'] = Carbon::now();
$asset_arr[$i]['updated_at'] = Carbon::now();
like image 77
Alexey Mezenin Avatar answered Mar 20 '23 19:03

Alexey Mezenin