I am trying to insert a two related objects via Eloquent and I get an Integrity constraint violation error on one of the tables. Here is what I am trying:
The representation of the relationship:
class DataParticipant extends Model
{
public function data_config()
{
return $this->hasOne('App\Models\DataConfig');
}
}
class DataConfig extends Model
{
public function data_participant()
{
return $this->belongsTo('App\Models\DataParticipant');
}
}
Then, I am trying the following:
$dataParticipant = new DataParticipant();
$dataParticipant->ip = // ip
$dataParticipant->code = // code
$dataParticipant->study_name = // study
// Prepare the config child.
$dataConfig = new DataConfig();
$dataConfig->config = // config via json_encode
// Store the child associated to the parent.
$dataParticipant->data_config()->save($dataConfig);
The error is on data_participant_id column, which belongs to the DataConfig model. My guess is that there is no id on which the two can be linked. I want to ask you:
- How can I accomplish this without having to first save the
DataParticipantthen retrieve it from the database and then store theDataConfigon the resultedid?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire