php – Display the creation date (created_at) in the post in Laravel 5.5

Question:

Good morning guys, I'm very new to laravel and I have a small problem that I'm not managing to solve. When viewing a post, I need to display the date the post was created. In the database it would be the created_at column (timestamp format). But I don't know why, this field is printing a null result. Can anyone tell me how to solve this? I was trying to use it in the following way:

{{ Carbon\Carbon::parse($noticia->created_at)->format('d/m/Y') }}

But the code above just displays the current date, I believe because the $noticia->created_at field is coming to null;

Here is the code for the complete site:

Model

namespace App\Models;        
use Illuminate\Database\Eloquent\Model;
    use App\Traits\Sluggable;

    class Post extends Model
    {
        use Sluggable;
        protected static $sluggable = 'titulo';
        public static $storage      = 'post';
        protected $table            = 'post';
        protected $dates            = ['periodo_inicio', 'periodo_fim', 'created_at', 'updated_at'];
        protected $fillable         = [
            'id', 'titulo', 'conteudo', 'periodo_inicio', 'periodo_fim', 'imagem', 'qtd_views',
            'active', 'destaque', 'slug', 'resumo', 'created_at', 'updated_at', 'title_seo', 'description_seo'
        ];
    }

Controller

public function noticiasDetalhe($slug)
    {
        $base_posts_destaques = Post::where('active', 1)->where('destaque', 1)->orderBy('created_at', 'desc')->take(4)->get();

        $noticia = Post::where('slug', $slug)->leftjoin('post_rel_categorias', 'post_rel_categorias.post_id', '=', 'post.id')->first();

        $buscar = '';
        $page_title = '';
        $page_description = '';

        if($noticia) {

            $noticia->update(['qtd_views'=>($noticia->qtd_views + 1)]);
            if($noticia->title_seo != "") {
                $page_title = $noticia->title_seo . " - Berkan";
            } else {
                $page_title = $noticia->titulo . " - Berkan";
            }

            if($noticia->description_seo != "") {
                $page_description = $noticia->description_seo . " - Berkan";
            } else {
                $page_description = $noticia->resumo . " - Berkan";
            }

            $categorias = Post_Categoria::where('active', 1)->get();

            $base_posts = Post::where('post_rel_categorias.post_categoria_id', $noticia->post_categoria_id)
                ->where('post_rel_categorias.id', '<>', $noticia->id)
                ->leftjoin('post_rel_categorias', 'post_rel_categorias.post_id', '=', 'post.id')
                ->select(
                    'post_rel_categorias.id',
                    'post_rel_categorias.post_id',
                    'post.id',
                    'post.titulo',
                    'post.imagem',
                    'post.slug',
                    'post.created_at'
            )->orderBy('created_at', 'desc')->take(3)->get();

        } else {
            abort(404);
            die();
        }

        return view('noticias-detalhe', compact('noticia', 'base_posts_destaques', 'base_posts', 'categorias', 'page_title', 'page_description'));
    }

If anyone knows how to solve this thank you.

print dd($news);

Post {#419 ▼
  #table: "post"
  #dates: array:4 [▼
    0 => "periodo_inicio"
    1 => "periodo_fim"
    2 => "created_at"
    3 => "updated_at"
  ]
  #fillable: array:15 [▼
    0 => "id"
    1 => "titulo"
    2 => "conteudo"
    3 => "periodo_inicio"
    4 => "periodo_fim"
    5 => "imagem"
    6 => "qtd_views"
    7 => "active"
    8 => "destaque"
    9 => "slug"
    10 => "resumo"
    11 => "created_at"
    12 => "updated_at"
    13 => "title_seo"
    14 => "description_seo"
  ]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:18 [▼
    "id" => "53"
    "titulo" => "Educação profissional continuada para o auditor independente"
    "slug" => "educacao-profissional-continuada-para-o-auditor-independente"
    "resumo" => "Como uma forma de valorizar a profissão do contador diante da sociedade e das empresas, o Conselho Federal de Contabilidade (CFC) estabeleceu a norma de educaçã ▶"
    "conteudo" => ""
    "imagem" => "post/N9kKOUODlFnISmF8pyO8hvJR2nDMWoLHjz5cAcmO.jpeg"
    "periodo_inicio" => null
    "periodo_fim" => null
    "active" => "1"
    "destaque" => "0"
    "qtd_views" => "272"
    "deleted_at" => null
    "created_at" => null
    "updated_at" => null
    "title_seo" => "Educação profissional continuada para o auditor independente"
    "description_seo" => "Como uma forma de valorizar a profissão do contador diante da sociedade e das empresas, o Conselho Federal de Contabilidade (CFC) estabeleceu a norma de educaçã ▶"
    "post_id" => "43"
    "post_categoria_id" => "5"
  ]
  #original: array:18 [▼
    "id" => "53"
    "titulo" => "Educação profissional continuada para o auditor independente"
    "slug" => "educacao-profissional-continuada-para-o-auditor-independente"
    "resumo" => "Como uma forma de valorizar a profissão do contador diante da sociedade e das empresas, o Conselho Federal de Contabilidade (CFC) estabeleceu a norma de educaçã ▶" 
    "imagem" => "post/N9kKOUODlFnISmF8pyO8hvJR2nDMWoLHjz5cAcmO.jpeg"
    "periodo_inicio" => null
    "periodo_fim" => null
    "active" => "1"
    "destaque" => "0"
    "qtd_views" => "272"
    "deleted_at" => null
    "created_at" => null
    "updated_at" => null
    "title_seo" => "Educação profissional continuada para o auditor independente"
    "description_seo" => "Como uma forma de valorizar a profissão do contador diante da sociedade e das empresas, o Conselho Federal de Contabilidade (CFC) estabeleceu a norma de educaçã ▶"
    "post_id" => "43"
    "post_categoria_id" => "5"
  ]
  #changes: []
  #casts: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}

Thanks

Answer:

As we saw in Chat the problem is related to the innerJoin that is subscribing the information in the news table,

$noticia = Post::where('slug', $slug)
    ->select('post.*')
    ->leftjoin('post_rel_categorias', 'post_rel_categorias.post_id', '=', 'post.id')
    ->first();

I added the select to be able to separate the inner data with the news data,

NOTE replace the post.* with the name of your table.

another tip to make your life easier and within the model

Add the following line:

protected $dates = [
        'created_at',
        'updated_at',
    ];

So when calling information for example:

$noticia->created_at; you can add the format method to return it formatted, for example:

$noticia->create_at->format('d/m/Y');
Scroll to Top