dousi1875 2016-06-02 05:51
浏览 90
已采纳

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

In my Controller I get the current date an another date, I send this params to my 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

I debug my laravel view and the lt() method receives a correct Carbon object

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

        );
  • 写回答

1条回答 默认 最新

  • doutan1905 2016-06-02 06:13
    关注

    Issue over here is with the $articles_row['expiration_date'] which is considered as a string and not an object of Carbon class. 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

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

    Updating OPs answer for Future Users

    (\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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里