@shiruken Timestamps are turned on by default so you shouldn't need to explicitly turn them, but in any case the property needs to be public.
public $timestamps = true;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey all!
I recently started trying to port over to Postgres from MySQL, and have bumped into a rather perplexing issue: My models aren't filling in timestamps.
Here's my relevant migration:
Schema::create('categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
$table->softDeletes();
});
Schema::create('category_nouns', function(Blueprint $table)
{
$table->increments('id');
$table->string('noun');
$table->integer('weight');
$table->integer('category_id')->unsigned()->default(0);
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
$table->softDeletes();
});
And here are my model setups:
class Category extends Model {
use SoftDeletes;
protected $timestamps = true;
protected $guarded = ['id'];
protected $table ='categories';
/* input requirements */
public static $rules = ['name'=>'required'];
class CategoryNouns extends Model {
use SoftDeletes;
protected $timestamps = true;
protected $guarded = ['id'];
protected $table ='category_nouns';
/* input requirements */
public static $rules = [
'noun' => 'required|unique:category_nouns',
'weight' => 'required|integer',
'category_id' => 'required|integer'
];
(the create methods have not been overwritten)
Here's the code:
// load file
$inputCsv = Reader::createFromPath($file);
//@TODO: Progress Bars @ http://laravel.com/docs/5.1/artisan#writing-output
// assume headers are categories
$headers = $inputCsv->fetchOne(0);
foreach($headers as $category) {
$cat = Category::firstOrCreate(['name'=>$category]);
$cats[$category] = $cat->id;
}
// load nouns and associate to their respective categories
$data = $inputCsv->setOffset(1)->fetchAssoc(array_flip($cats)); // skip headers now
foreach($data as $row) {
foreach($row as $cat_name=>$noun) {
$noun_exists = CategoryNouns::where('noun','=',$noun)->first();
if(!empty($noun) && !isset($noun_exists->exists)) {
$cat_noun = CategoryNouns::create(['noun' => $noun, 'weight'=> 1, 'category_id' => $cats[$cat_name]]);
}
}
}
$this->comment("Done.");
And finally, here's the error I keep getting:
[Illuminate\Database\QueryException]
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "created_at" violates not-null constraint
DETAIL: Failing row contains (2, Utilities, null, null, null). (SQL: insert into "categories" ("name") values (Utilities) returning "id")
Any help would be appreciated. Thanks in advance!
So it turns out I was editing the Models from a different branch, stored in a folder named almost exactly like the one I was trying to work with...
Frustration is a helluva blinder.
Please or to participate in this conversation.