php - 在 Laravel 5.5 的帖子中显示创建日期(created_at)
问题描述
大家早上好,我是 laravel 的初学者,我有一个无法解决的小问题。在查看帖子时,我需要显示帖子的创建日期。在数据库中将是 created_at 列(时间戳格式)。但我不知道为什么,这个字段正在打印一个空结果。谁能告诉我如何解决这个问题?我试图这样使用它:
{{ Carbon\Carbon::parse($noticia->created_at)->format('d/m/Y') }}
但是上面的代码只显示当前日期,我相信字段 $ noticia-> created_at 即将为空;
以下是完整的网站代码:
模型
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'
];
}
控制器
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'));
}
如果有人知道如何解决这个问题,谢谢。
打印 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 => "*"
]
}
谢谢
解决方案
从 $dates 和 $fillable In 刀片中删除您的 created_at 和 updated_at 只需打印出来
{{ $noticia->created_at->format('d/m/Y') }}
推荐阅读
- php - 在 PHP 7.2 开发服务器上使用 router.php 脚本来处理 .htaccess mod 重写
- python - “VoiceClient”对象没有属性“create_ffmpeg_player”
- gitkraken - 远程“Origin”不支持 LFS 锁定 API
- debian-based - 是否可以在 Debian 上运行 RAPIDS?
- c# - 方法执行后丢失引用
- java - 3 维房间中的欧几里得距离 - 基于 Java 中的 2d 数组
- c# - 如何加载图像,将其转换为 jpg 格式并调整其大小
- react-native - 每当我尝试将选项卡导航添加到我的 react-native 应用程序时,我都会收到以下错误“(0 , _reactNavigation.TabNavigator) 不是函数”
- batch-file - 如果文件大小超过 0 KB,如何将文件从源复制到目标?
- python - 如何使用python在Tensorflow、CNN中创建学习模型的多个实例?