dongyinzhi4689 2019-01-23 10:51
浏览 269

在Laravel 5.5中显示帖子中的创建日期(created_at)

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:

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

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.

print dd($noticia)

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 => "*"
  ]
}

Thank you

  • 写回答

1条回答 默认 最新

  • dongshipang8094 2019-01-23 19:47
    关注

    Remove your created_at and updated_at from $dates and $fillable In blade simply print out

    {{ $noticia->created_at->format('d/m/Y') }}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题