S
S
SimonPomidorkin2018-07-06 11:50:58
Laravel
SimonPomidorkin, 2018-07-06 11:50:58

How to disable automatic insertion of data in "created_at" in laravel?

The essence of the problem is this: I'm trying to bulk insert data into a database table through laravel, but the table does not have columns "created_at" and "updated_at", however, laravel tries to automatically insert data into these columns, does not find them and, therefore, gives an error that such columns do not exist.
Can you please tell me how to disable automatic insertion of data in the mentioned columns?

address.php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    protected $fillable = ['name'];
}
web.php
Route::get('/insert', function () {

    $user = User::findOrFail(1);
    $address = new Address(['name'=>'Улица Пушкина, Дом Колотушкина']);
    $user->address()->save($address);

});
user.php
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function address(){
        return $this->hasOne('App\Address');
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Vasin, 2018-07-06
@SimonPomidorkin

Specify in the model public $timestamps = false;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question