Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PetroGromovo's avatar

Why model of new created row has created_at == null?

In lumen 8 when I add new row in pages table on success

            $page = Page::create($this->requestData);
            \Log::info(varDump($page, ' -12 $page::'));

all fields in $page have values, except created_at, as in debugging output

[2021-05-29 10:15:14] local.INFO:  (Model Object of App\Models\Page) : -12 $page:: : Array
(
    [title] => New page title
    [content] => New page title content Lorem ipsum dolor sit amet consectetur adipisicing elit. A
                    ullam expedita, obcaecati itaque, perspiciatis aliquam tempora magni harum exercitationem
    [content_shortly] => New page title content shortly
    [creator_id] => 1
    [is_homepage] => 0
    [published] => 1
    [image] => 
    [meta_description] => New page title meta description
    [meta_keywords] => ["value","value2","value3"]
    [slug] => new-page-title
    [id] => 11
)

But created_at is defined as current in db :

  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

and I see valid value in db table, but not in the model... Looks like somewhat it was lost in the model app/Models/Page.php :

<?php
namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
use Illuminate\Support\Str;
use Cviebrock\EloquentSluggable\Sluggable;
use \Cviebrock\EloquentSluggable\Services\SlugService;

class Page extends Model
{
    use Sluggable;
    protected $table = 'pages';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $dates = ['created_at', 'updated_at'];

    protected $pageImagePropsArray = [];

    protected static $uploads_page_dir = 'pages/-page-';

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }


    protected $fillable = [
        'title', 'content', 'content_shortly', 'creator_id', 'is_homepage', 'published', 'image', 'meta_description', 'meta_keywords'
    ];


    public function creator(){
        return $this->belongsTo('App\Models\User', 'creator_id','id');
    }

    public function scopeGetById($query, $id)
    {
        return $query->where(with(new Page)->getTable() . '.id', $id);
    }

    public function scopeGetByTitle($query, $title = null)
    {
        if (empty($title)) {
            return $query;
        }
        return $query->where( with(new Page)->getTable() . '.title', 'like', '%'.$title.'%');
    }

    public function scopeGetBySlug($query, $slug = null)
    {
        if (empty($slug)) {
            return $query;
        }
        return $query->where(with(new Page)->getTable() . '.slug', $slug);
    }


    public function scopeGetByIsHomepage($query, $is_homepage = null)
    {
        if (!isset($is_homepage) or strlen($is_homepage) == 0) {
            return $query;
        }
        return $query->where(with(new Page)->getTable().'.is_homepage', $is_homepage);
    }

    public function scopeGetByPublished($query, $published = null)
    {
        if (!isset($published) or strlen($published) == 0) {
            return $query;
        }
        return $query->where(with(new Page)->getTable().'.published', $published);
    }

    public function scopeOnlyPublished($query) {
        return $query->where('published', true);
    }


    protected static function boot()
    {
        parent::boot();

        static::deleting(function($page) {
            $pageImageImagePath = Page::getPageImagePath($page->id, $page->image);
            deleteFileByPath($pageImageImagePath, true);
        });

        static::updating(function($pageItem) {
            $slug = SlugService::createSlug(Page::class, 'slug', $pageItem->title);
            $pageItem->slug = Str::slug($slug);
        });

    }

}

Why created_at has null value and how it can be fixed ?

Thank you in advance!

0 likes
2 replies

Please or to participate in this conversation.