简体   繁体   English

laravel 5.4 count()函数错误

[英]laravel 5.4 count() function error

I am using laravel 5.4 , there is an error in the code below, 我正在使用laravel 5.4 ,下面的代码有错误,
but there is no error when using laravel 5.3 , 但是使用laravel 5.3时没有错误,
why is it? 为什么?

controller: 控制器:

public function index()
{
    $user=\Auth::user();
    $articles=$user->articles;

    return view('index',  compact('user','articles'));
}

view: 视图:

{{ $articles->count() }}

error: 错误:

Call to a member function count() on null

what should I do? 我该怎么办?

Laravel 5.4您应该尝试:

{{ count($articles) }}

Change it to count($articles) . 将其更改为count($articles) ->count() on a null value produces indeed an error. ->count()为空值确实会产生错误。

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

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