繁体   English   中英

不能在我的laravel .blade视图中使用Carbon Laravel方法

[英]Can't use Carbon Laravel method in my laravel .blade view

在我的控制器中,我将当前日期另一个日期,我将此params发送到我的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中的FatalErrorException 112:调用字符串上的成员函数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',
                )),
            ),

        );

这里的问题是$articles_row['expiration_date'] ,它被认为是一个string而不是Carbon类的对象。 所以解决的问题是,你需要使用parse方法为你的$articles_row['expiration_date']创建一个Carbon类对象,如as

$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对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM