I want to seed the hero
table, but I get the following error.
Argument 1 passed to Illuminate\Database\Query\Builder::insertGetId() must be of the type array, object given, called in
E:\xampp\htdocs\cyberpunk\vendor\laravel\framework\ src\Illuminate\Database\Eloquent\Builder.php on line 1350.
Exception trace: Illuminate\Database\Query\Builder::insertGetId(Object(App\Hero), "id") ...vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:1350
Seeder
User::find(1)->hero()->create([
'name' => $faker->userName,
'level' => $faker->numberBetween(1, 99),
'strength' => $faker->numberBetween(1, 20),
'vitality' => $faker->numberBetween(1, 20),
'stamina' => $faker->numberBetween(1, 20),
'agility' => $faker->numberBetween(1, 20),
'perception' => $faker->numberBetween(1, 20),
'luck' => $faker->numberBetween(1, 20),
]);
I have a one-to-one relation between User
and Hero
. I want to create a new hero
via the user
model. This way also causes the same error.
$hero = new Hero;
$hero->user_id = 1;
...
$hero->save();
User
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password'
];
protected $hidden = [
'password', 'remember_token',
];
public $appends = ['hashid'];
public function hero()
{
return $this->hasOne(Hero::class);
}
}
Hero Model
class Hero extends Model
{
protected $fillable = ['level'];
public function user()
{
return $this->belongsTo(User::class);
}
}