php 在非对象上调用成员函数 count() (Laravel 5)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29941759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function count() on a non-object (Laravel 5)
提问by Liam
I have a list of projects, where I can click on and it (needs to) show the project name, and the list of the projects' tasks (ToDo application)
我有一个项目列表,我可以在其中单击它(需要)显示项目名称和项目任务列表(ToDo 应用程序)
But when I click on a certain project I get this error.
但是当我单击某个项目时,出现此错误。
(The "H2" project name will show up)
(将显示“H2”项目名称)
h2>{{{ $project->name }}}</h2>
@if ( !$project->tasks->count())
There are no tasks for this project.
@else
<ul>
@foreach( $project->tasks as $task)
<li><a href="{{ route('projects.tasks.show', [$project->slug, $task->slug]) }}">{{ $task->name }}</a></li>
@endforeach
</ul>
@endif
回答by Sougata Bose
$project->tasks
is an array and you cant call count()
on the array. So try with -
$project->tasks
是一个数组,您不能调用count()
该数组。所以尝试 -
count($project->tasks)
回答by Ovidiu Badita
Try count($project->tasks)==0
尝试计数($project->tasks)==0
h2>{{{ $project->name }}}</h2>
@if ( count($project->tasks)==0)
There are no tasks for this project.
@else
<ul>
@foreach( $project->tasks as $task)
<li><a href="{{ route('projects.tasks.show', [$project->slug, $task->slug]) }}">{{ $task->name }}</a></li>
@endforeach
</ul>
@endif
回答by Sambbhav Garg
I see your issue is resolved but this might help someone else :D
我看到您的问题已解决,但这可能对其他人有所帮助:D
The following piece of code might work for you, but it's not that we can't call count() on the array.
下面的一段代码可能对你有用,但并不是我们不能在数组上调用 count() 。
count($project->tasks)
Instead of this
而不是这个
h2>{{{ $project->name }}}</h2>
@if ( !$project->tasks->count()) // tasks is plural -> wrong.
There are no tasks for this project.
@else
<ul>
@foreach( $project->tasks as $task) // here too!
<li><a href="{{ route('projects.tasks.show', [$project->slug, $task->slug]) }}">{{ $task->name }}</a></li>
@endforeach
</ul>
@endif
Try doing this
尝试这样做
h2>{{{ $project->name }}}</h2>
@if ( !$project->task->count())
There are no tasks for this project.
@else
<ul>
@foreach( $project->task as $task)
<li><a href="{{ route('projects.tasks.show', [$project->slug, $task->slug]) }}">{{ $task->name }}</a></li>
@endforeach
</ul>
@endif