php - laravel报错讲非法标签设置或值为空?
本文介绍了php - laravel报错讲非法标签设置或值为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
报错信息如下:
ErrorException in Factory.php line 159:
Illegal offset type in isset or empty
具体是:视图:
<p class="post-meta">
Posted on {{ $post->publish_at->format('F j Y') }}
@if ($post->grades->count())
in
{!! join(', ', $post->gradeLinks()) !!}
@endif
</p>
模型model里的对应函数:
public function gradeLinks($base = '/article_system?grade=%grade%')
{
$grades = $this->grades()->lists('grade');
$return = [];
foreach ($grades as $grade) {
$url = str_replace('%grade%', urlencode($grade), $base);
$return[] = '<a href="'.$url.'">'.e($grade).'</a>';
}
return $return;
}
控制器:
public function index(Request $request)
{
$grade = $request->get('grade');
$data = $this->dispatch(new Article_systemIndexData($grade));
$layout = $grade ? Grade::layout($grade) : 'article_system.layouts.index';
echo $grade;
echo $layout;
return view($layout, $data);
路由:
Route::get('/', function () {
return redirect('/article_system');
});
Route::get('article_system', 'article_systemController@index');
Route::get('article_system/{slug}', 'article_systemController@showPost');
问题中的报错是在我点开对应gradelinks的那个标签时给的报错,另外,我写的那个任务队列Article_systemIndexData没有bug,在别的视图里用到这个job的地方都没报错,望大神指点迷津啊、、、、、orz
解决方案
使用日期格式化时,需要先在model中设置dates属性:
protected $dates = ['publish_at'];
然后格式化这样写:
{{ $post->publish_at->format('F j, Y') }}
这篇关于php - laravel报错讲非法标签设置或值为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文