简体   繁体   中英

laravel 5.4 count() function error

I am using laravel 5.4 , there is an error in the code below,
but there is no error when using 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() on a null value produces indeed an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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