How to write the syntax for judging whether the returned collection is empty in Laravel5.3 view?
巴扎黑
巴扎黑 2017-05-16 16:51:30
0
2
450

Using laravel 5.3, there is a problem with the @if judgment in the view below:
When the user has no articles, the content in @if will still be executed instead of the content in @else, that is, " "There are articles", is $articles!= null in the view written incorrectly?

Controller:

$user=\Auth::user();
$articles = $user->articles;
return view('articles.index',  compact('articles'));

view:

@if ($articles!= null)
<p>有文章</p>
@else
<p>没有文章</p>
@endif
巴扎黑
巴扎黑

reply all(2)
过去多啦不再A梦

Already using 5.3?

Collection has an isEmpty method to determine whether the Collection is empty. You should be able to use $articles->isEmpty() to judge.
See the official API documentation for details: https://laravel.com/api/5.0/I...

Introduction in official documentation:

isEmpty()
Determine if the collection is empty or not.
曾经蜡笔没有小新

I haven’t used laravel 5.3 yet
But the returned collection cannot be judged by Null
Because even if it is empty, it will return the collection object
You can use $articles->count() or $articles->first()

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!