我正面临播种问题。我的AddressessTableSeeder.php
文件如下所示
use Illuminate\Database\Seeder;
class AddressessTableSeeder extends Seeder
{
public function run()
{
factory(App\Address::class, 50)->create()->each(function ($u)
{
$u->users()->save(factory(App\User::class)->make());
});
}
}
我的Address.php
文件如下所示
class Address extends Model
{
protected $fillable = ['user_id', 'name', 'address','mobile_no','image','email'];
public function users() {
return $this->belongsTo(User::class);
}
}
我收到如下错误
答案 0 :(得分:1)
使用associate
并在关联之前创建用户,这样您就可以在保存之前获取ID
factory(App\Address::class, 50)->create()->each(function ($u)
{
$u->users()->associate(factory(App\User::class)->create())->save();
});