BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::incomplete()'
I get this error when I try to call the function incomplete from my Task model using tinker.
Code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
public static function incomplete()
{
return static::where('completed','=',0)->get();
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
public function scopeIncomplete($query)
{
return$query->where('completed', 0);
}
}
Then, you can reuse any time you need incomplete task(s)
I was watching episode 7 of 'Laravel 5.4 from scratch', so I didn't yet know about query scopes. Besides, this may fix my problem, but I'm still wondering why I get that error
Honestly, I don't know. If the code is as you have describe then the class should respond to incomplete() and not pass it up to the Query Builder. I would double check spellings, correct use of statickeyword. Beyond that, I would be at a loss without seeing more code.
I had exactly the same error. I spend the evening to find out what was wrong with my code. I thought that it was because of my version of Laravel (5.6.).
And finally, I just had to kill the tinker process and restart everything, and it did the trick.
If I change my code in the model, for example, I want to display the completed tasks (put 1 instead of 0), I change the code, then I need to kill the process, and restart it to get what it is expected. Otherwise, it doesn't work.