I am getting the following error when I try to save a new property:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (lightning.residentials, CONSTRAINT residentials_residential_type_id_foreign FOREIGN KEY (residential_type_id) REFERENCES residential_types (id)) (SQL: insert into residentials (furnished, rooms, rent, property_id, updated_at, created_at) values (1, 1, 100, 17, 2016-07-06 23:32:59, 2016-07-06 23:32:59))
I cant see why.
The Property model:
protected $table = 'properties';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id', 'is_for_sale','available','title', 'description','price'
];
public function user()
{
return $this->belongsTo('App\User');
}
public function Residential()
{
return $this->hasOne('App\Residential');
}
/**
* The Addresses that belong to the properties.
*/
public function addresses()
{
return $this->belongsToMany('App\Address');
}
address model
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'building_name', 'street_number', 'street_name', 'town', 'county', 'postcode','is_deleted'
];
/**
* The Users that belong to the Address.
*/
public function user()
{
return $this->belongsToMany('App\User');
}
/**
* The Users that belong to the Address.
*/
public function organisation()
{
return $this->belongsToMany('App\Organisation');
}
/**
* The Users that belong to the Address.
*/
public function Property()
{
return $this->belongsToMany('App\Properties');
}
The residential model:
protected $table = 'residentials';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'property_id', 'residential_type_id','furnished','rooms','rent'
];
public function Property()
{
return $this->belongsTo('App\Property');
}
public function Residential_Type()
{
return $this->belongsTo('App\Residential_types');
}
The residential_type model
// Define table name
protected $table = 'residential_types';
protected $fillable = [
'residential_type'
];
/**
*
*/
public function Residential()
{
return $this->hasMany('App\Residential', 'residential_type_id');
}
My controller function where I try to save a property along with the fields in the related residential table but I get the error above. The property and address saves correctly just fails when it comes to the residential.
$property = new Property([
'user_id' => $user->id,
'is_for_sale' => $request['listing_type'],
'available' => $request['available'],
'title' => $request['title'],
'description' => $request['description'],
'price' => $request['price'],
]);
$property->save();
//save address and related to property
$address = new Address([
'building_name' => $request['building_name'],
'street_number' => $request['street_number'],
'street_name' => $request['street_name'],
'town' => $request['town'],
'county' => $request['county'],
'postcode' => $request['postcode'],
]);
$property->addresses()->save($address);
//save property and related to residential
$residential = new Residential([
'furnished' => $request['furnished'],
'rooms' => $request['bedrooms'],
'rent' => $request['price'],
]);
$residential_type = new Residential_Types([
'residential_type' => $request['property_type'],
]);
$residential_type->save();
$property->Residential()->save($residential);
$residential->Residential_Type()->save($residential_type);
return view('renting');
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire