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

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>{{ $task->name }}</li>
#endforeach
</ul>
#endif

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

Try count($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>{{ $task->name }}</li>
#endforeach
</ul>
#endif

I see your issue is resolved but this might help someone else :D
The following piece of code might work for you, but it's not that we can't call count() on the array.
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>{{ $task->name }}</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>{{ $task->name }}</li>
#endforeach
</ul>
#endif

I use collection for empty objects this could work better
in my controller
if ($search) {
$taks = MODEL::search($search)->get();
} else {
$tasks = collect([]);
}
in my view
#if ($tasks->count())
<ul class="list-group">
#foreach($tasks as $task)
<li>info and action</li>
#endforeach
</ul>
#else
<p class="text-danger">Error message</p>
#endif

Related

How to group by in laravel with related table?

This is my blade file
<ul>
#foreach ($cajeros as $cajero)
<li>
{{$cajero->idcajero}}
<ul>
#foreach ($cajero->conciliaciones as $conciliacion)
<li>Lote {{ $conciliacion->lote }} - ${{ $conciliacion->importe_comisionable }}</li>
#endforeach
Total cajero: ${{ $cajero->conciliaciones->sum('importe_comisionable') }}
</ul>
</li>
#endforeach
And this is the result
What I want to do is group by lote and get the sum of that.
I tried doing
#foreach ($cajero->conciliaciones->groupby('lote') as $key => $conciliaciones)
<li>Lote {{ $key }} - ${{ sum($conciliaciones) }}</li>
#endforeach
and I got
Call to undefined function sum()
Also, I tried doing it on the controller. But I get the sum as zero.
$detalle = [];
foreach ($cajeros as $cajero){
$detalle[] = [$cajero->id => $cajero->conciliaciones->groupby('lote')->sum('importe_comisionable')];
}
I solved it with
<ul>
#foreach ($cajeros as $cajero)
<li>
{{$cajero->idcajero}}
<ul>
#foreach ($cajero->conciliaciones->groupby('lote') as $key => $conciliaciones)
#php $suma = 0; #endphp
#foreach ($conciliaciones as $conciliacion)
#php $suma += $conciliacion->importe_comisionable; #endphp
#endforeach
<li>Lote {{ $key }} - ${{ $suma }}</li>
#endforeach
Total cajero: ${{ $cajero->conciliaciones->sum('importe_comisionable') }}
</ul>
</li>
#endforeach

How can I get information about errors in laravel 6.0?

When I used Laravel 5.8 I could get information about errors like this :
#if(count($errors > 0))
<ul class="error-list" >
#foreach($errors->all() as $error)
<li class="error-list-element">{{$error}}</li>
#endforeach
</ul>
#endif
It worked completly fine.
Since Laravel 6.0 was released the code above results in error :
Object of class Illuminate\Support\ViewErrorBag could not be converted to int
So how can I get information about errors in Laravel 6.0?
Here is code :
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Hope this will help :)
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

need to get next Task in foreach

My For each return the task same as in URL Now I need to get the next task
#foreach ($Tasks as $Task)
#if ($Task->slug == Request::segment(5))
<h2>{{ $Task->task_name }}</h2>
#endif
#if ($loop->remaining)
<p>Next Task is: {{ $Tasks[$loop->iteration]->task_name }}</p>
#endif
#endforeach
I get the task in URL so I need to get next task in foreach but it returns empty
and tried
#foreach ($ATask as $key => $Task)
#if ($ATask[$key]->slug == Request::segment(5))
<h2>{{ $Task->task_name }}</h2>
<p>Next Task is: {{ $ATask[$key++]->task_name }}</p>
#endif
#endforeach
Maybe this script can slove your problem:
#foreach ($Tasks as $key => $Task)
#if ($Task->slug == Request::segment(5))
<h2>{{ $Task->task_name }}</h2>
<p>Next Task is: {{ $Tasks[$key+1]->task_name }}</p>
#endif
#endforeach
Hope it helps.
Also you can use $key++ instead of $key+1

How to perform this operation in a view with Query builder and pivot table

I have a "teachers" table and a "courses" table. I get to access my "courses" data when I'm on a "teachers" view with a foreach.
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
But I would like to hide the section if it "teachers" has no "course".
#if(teacher_has_course)
<section class="My section">
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
</section>
#endif
I do not understand the principle. Can you help me ? Thank you
you can check like this:
#if(!empty($teacher->courses) && count($teacher->courses))
<section class="My section">
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
</section>
#endif
<section class="My section">
#foreach($teacher->courses as $course)
#unless(!$course)
<li>{{ $course->title }}</li>
#endunless
#endforeach
</section>
or
#unless(!$teacher->courses)
<section class="My section">
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
</section>
#endunless
If you have defined a relation between two tables you can use ->has() method. Lets say there is one to many relation between 'teachers' and 'courses' then you can check like this
#if($teacher->has('course'))
// html code that you want to show
#endif
Refer https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-existence
Hope this helps

Adding in code snippets/partials on a page

How do I include snippets, or partials in pages in Laravel 5? In node/angular, it's quite easy to simply load different modules and such on a page.
For example, on my home page, I'm looping through some data:
<h1>Home</h1>
#if (count($practice))
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
#endif
If I include the login snippet on the page, it covers up the rest of the data:
<h1>Home</h1>
#if (count($practice))
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
#endif
#include('auth.login')
Try to include it in a new row :
<h1>Home</h1>
#if (count($practice))
<div class="row">
<div class="col-md-12">
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
</div>
</div>
#endif
<div class="row">
<div class="col-md-12">
#include('auth.login')
</div>
</div>

Categories