0
votes

I am trying to do php artisan db:seed on Laravel 5.6.39 and getting an error:

BadMethodCallException : Method Illuminate\Database\Query\Builder::offers does not exist.

Offer Model code:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Offer extends Model
{
    protected $guarded = [];

    public function task()
    {
        return $this->belongsTo(Task::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

}

Task Model

public function offers()
{
    return $this->hasMany(Offer::class);
}

Databaseseeder file

factory(App\Task::class, 10)->create()->each(function ($task) {
    return $task->offers()->save(factory(App\Offer::class)->make());
});

What am I not doing right?

1
well it seems to think there isn't an offers method on App\Tasklagbox
@lagbox could u be more elaborate? thx,Daniel Omara
well the first thing is how do you know that the code you are showing from the seeder is causing this?lagbox
When I comment out the code below and run db:seed, the error does not occur factory(App\Task::class, 10)->create()->each(function ($task) { return $task->offers()->save(factory(App\Offer::class)->make()); });Daniel Omara
PHP doesn't seem to think that App\Task has an offers method or the visibility isn't publiclagbox

1 Answers

-1
votes

Update your DatabaseSeeder to this and check

factory(App\Task::class, 10)->create()->each(function ($task) {
    return $task->offers()->save(factory(App\Offer::class)->create());
});