在我的控制器中,我将当前日期另一个日期,我将此参数发送到我的view.blade:
$current_date = Carbon::now('Europe/Madrid');
$second_date = Carbon::create(2016, 6, 3, 00, 00, 00, 'Europe/Madrid');
在我的控制器中,此功能正常工作:
$current_date->lt($second_date) # true
但在我的blade.php中Laravel报告此错误: efcb37f79f0e8cb86e0f747997c402d6f7026ada.php第112行中的FatalErrorException: 在字符串
上调用成员函数lt()我调试我的laravel视图,而lt()方法接收正确的Carbon对象
Carbon {#361 ▼
+"date": "2016-06-03 00:00:00.000000"
+"timezone_type": 3
+"timezone": "Europe/Madrid"
}
这里发生了什么?
我的观看代码:
@foreach ($articles_rows as $articles_row)
@if($current_date->lt($articles_row['expiration_date']))
<div class="row">
...show articles here
</div>
@endif
@endforeach
我的控制器代码:
$articles_rows = array (
array(
'img_src' => FuImg::asset('img/regalos-para-profesores.jpg'),
'img_alt' => 'Regalos para profesores',
'expiration_date' => Carbon::create(2016, 6, 3, 00, 00, 00, 'Europe/Madrid'),
'class' => 'teacher-gifts',
'articles' =>
# Articulos
FotiArticle::getList(array(
'fields' => 'article_id,name,group_id,group,price_pvp_currency,quantity,prices,stock_available',
'expand' => 'group,prices',
'article_id' => '2829,186,2875,1728',
'order' => 'PRICE',
)),
),
array(
'img_src' => Img::asset('img/regalos-ocasiones-especiales.jpg'),
'img_alt' => 'Ocasiones especiales',
'expiration_date' => Carbon::create(2016, 6, 2, 00, 00, 00, 'Europe/Madrid'),
'class' => 'wedding',
'articles' => Article::getList(array(
'fields' => 'article_id,name,group,price_pvp_currency,quantity,prices,stock_available',
'expand' => 'prices',
'article_id' => '1611,4481,50,5345',
'order' => 'PRICE',
)),
),
);
答案 0 :(得分:3)
此处的问题是$articles_row['expiration_date']
被视为string
,而不是Carbon
类的对象。因此,解决的问题是,您需要使用Carbon
方法为$articles_row['expiration_date']
创建parse
类的对象,如
$current_date->lt(\Carbon\Carbon::parse($articles_row['expiration_date']))
为未来用户更新OP答案
(\Carbon\Carbon::parse($current_date)->lt($articles_row['expiration_date']))
OP在其自己的变量$current_date
中面临问题,因为问题是在逻辑上正确date
不是Carbon
对象。