简体   繁体   English

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

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

In my Controller I get the current date an another date, I send this params to my view.blade: 在我的控制器中,我将当前日期另一个日期,我将此params发送到我的view.blade:

$current_date = Carbon::now('Europe/Madrid');
$second_date  = Carbon::create(2016, 6, 3, 00, 00, 00, 'Europe/Madrid');

In my Controller this function works fine: 在我的控制器中,此功能正常工作:

$current_date->lt($second_date) # true

But in my blade.php Laravel report this error: FatalErrorException in efcb37f79f0e8cb86e0f747997c402d6f7026ada.php line 112: Call to a member function lt() on string 但是在我的blade.php中,Laravel报告了这个错误: efcb37f79f0e8cb86e0f747997c402d6f7026ada.php中的FatalErrorException 112:调用字符串上的成员函数lt()

I debug my laravel view and the lt() method receives a correct Carbon object 我调试我的laravel视图,而lt()方法接收一个正确的Carbon对象

Carbon {#361 ▼
  +"date": "2016-06-03 00:00:00.000000"
  +"timezone_type": 3
  +"timezone": "Europe/Madrid"
}

what is happening here? 这里发生了什么?

My view code: 我的观点代码:

@foreach ($articles_rows as $articles_row)
            @if($current_date->lt($articles_row['expiration_date']))
                <div class="row">
                ...show articles here
                </div>
            @endif
@endforeach

My controller code: 我的控制器代码:

$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',
                )),
            ),

        );

Issue over here is with the $articles_row['expiration_date'] which is considered as a string and not an object of Carbon class. 这里的问题是$articles_row['expiration_date'] ,它被认为是一个string而不是Carbon类的对象。 So the work around over is, you need to create an object of Carbon class for your $articles_row['expiration_date'] using parse method like as 所以解决的问题是,你需要使用parse方法为你的$articles_row['expiration_date']创建一个Carbon类对象,如as

$current_date->lt(\Carbon\Carbon::parse($articles_row['expiration_date']))

Updating OPs answer for Future Users 更新未来用户的OP答案

(\Carbon\Carbon::parse($current_date)->lt($articles_row['expiration_date']‌​))

OP is facing issue within its own variable $current_date as the issue is but logically correct date is not Carbon object. OP在其自己的变量$current_date面临问题,因为问题在于逻辑上正确的date不是Carbon对象。

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

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