Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function count() on a non-object (Laravel 5)

Tags:

php

laravel

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)

But when I click on a certain project I get this error.

(The "H2" project name will show up)

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
like image 534
Leguam Avatar asked Apr 29 '15 11:04

Leguam


1 Answers

$project->tasks is an array and you cant call count() on the array. So try with -

count($project->tasks)
like image 162
Sougata Bose Avatar answered Oct 23 '22 19:10

Sougata Bose