It appears that updating Laravel from 5.2 to 5.4 did the trick and fixed the error !
Error on dedicated query string after Homestead update (PHP 7.2 ask for Countable implementation)
Hi,
I followed the tutorial on Laracasts to make a dedicated query string filter for Eloquent. (https://laracasts.com/series/eloquent-techniques/episodes/4)
Everything was working perfectly, since I upgraded Homestead to a new version (especially a new version of PHP 7.2).
The version of the Laravel framework I did use is 5.2. I'm planning to upgrade it to the latest 5.5 but here I think the error is more of a change in PHP.
So the error is coming from this line :
$Musics = Music::with(['group', 'genres', 'actors', 'directors'])->filters($filters)->simplePaginate(18);
And the error I get is the following :
ErrorException in Builder.php line 1185:
count(): Parameter must be an array or an object that implements Countable
in Builder.php line 1185
at HandleExceptions->handleError('2', 'count(): Parameter must be an array or an object that implements Countable', '/home/vagrant/server/MusicTheater/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php', '1185', array('scope' => array(object(Music), 'scopeFilters'), 'parameters' => array(object(Builder), object(MusicFilters)), 'query' => object(Builder)))
at count(null) in Builder.php line 1185
at Builder->callScope(array(object(Music), 'scopeFilters'), array(object(Builder), object(MusicFilters))) in Builder.php line 1419
at Builder->__call('filters', array(object(MusicFilters))) in MusicController.php line 36
at MusicController->index(object(MusicFilters), object(Request))
at call_user_func_array(array(object(MusicController), 'index'), array(object(MusicFilters), object(Request))) in Controller.php line 80
at Controller->callAction('index', array(object(MusicFilters), object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(MusicController), object(Route), 'index') in ControllerDispatcher.php line 94
I have my filter class here :
<?php
namespace App;
class MusicFilters extends QueryFilter {
public function genre($genre)
{
return $this->getRelationManyToMany('genre')
->select('Musics.*', 'genres.name')
->where('genres.name', $genre);
}
public function year($year)
{
return $this->builder->where('year', '=', $year);
}
public function title($title)
{
return $this->builder->where('title', 'like', '%'.$title.'%');
}
public function getRelationManyToMany($otherTable)
{
$singular = $otherTable;
$plural = str_plural($otherTable);
return $this->builder->join($singular.'_Music', 'Musics.id', '=', $singular.'_Music.Music_id')
->join($plural, $otherTable.'_Music.'.$singular.'_id', '=', $plural.'.id');
}
}
And the QueryFilter class from Jeffrey that I inherit :
<?php namespace App;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
abstract class QueryFilter
{
protected $request;
protected $builder;
/**
* QueryFilter constructor.
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
public function apply(Builder $builder)
{
$this->builder = $builder;
foreach ($this->filters() as $name => $value) {
if (method_exists($this, $name) && array_filter([$value])) {
call_user_func_array([$this, $name], array_filter([$value]));
}
}
return $this->builder;
}
public function filters()
{
return $this->request->all();
}
}
So If I understand well the error is that It appears we are trying to iterate on an empty array, or a null object and it ask to implement the Countable interface on my filter class or the QueryFilter one.
In the code I see some iteration on the apply method in the QueryFilter abstract class but just to try I added some dd() in the beginning of the apply's method and the error is happening before.
Could someone help me finding what tweaks to make this work again. Since this code was working perfectly before the Homestead update.
Thanks in advance for your help !
Please or to participate in this conversation.