mercredi 23 janvier 2019

Display creation date (created_at) in post in Laravel 5.5

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



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

Here is the complete site code:

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.

Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire