0
I’m having trouble inserting into two related tables, the Peoples and Addresses tables.
Insertion is being done only in the Peoples table.
The relation in the Addresses table is made between the 'id' of Peoples and 'peoples_id' of Adresses. That is a one-to-many relationship.
Request data is obtained from a form that contains both People and Address fields.
Code of the People class
class People extends Model
{
protected $fillable = [
'photo',
'name',
'email',
'phone',
'tel',
'nationality',
'naturalness',
'birth_date',
'cpf',
'sex',
'rg',
'marital_status',
'election_title',
'election_section',
'electoral_zone',
'dad_name',
'mother_name',
];
protected $table = 'peoples';
public function address () {
return $this->hasMany(Address::class, 'people_id');
}
}
Code of class Address
class Address extends Model
{
protected $fillable = [
'people_id',
'cep',
'uf',
'country',
'city',
'district',
'public_place',
'number_home',
'complement',
];
protected $table = ['addresses'];
public function people () {
return $this->belongsTo(People::class, 'people_id');
}
}
Controller code:
public function store(Request $request, People $modelPeople, Address $modelAddress)
{
$modelPeople->create($request->all());
}