Answer the question
In order to leave comments, you need to log in
Eloquent ORM in Laravel?
There is a database, it has 2 tables (peoples, informations) connected by a foreign key (in this case it is id) there is also one data entry form. it turns out so far with only one table. Do I need to create two models for this, or register connections in the class method of our model? I would be very grateful if you could describe practically how this is implemented in laravel.
Answer the question
In order to leave comments, you need to log in
More or less like this
$people = People::find($id)->with('Information')->first();
$people->name = Input::get('name');
$people->Information->info = Input::get('info');
$people->save();
$people->Information->save();
Make data separation at the form level:
<input name="people[peoplData]" type="text">
...
<input name="information[informationData]" type="text">
I would recommend two models, if I understand you correctly. We create two models People and Information in
the People model, write the relationship
public function informations()
{
return $this->belongsToMany('Information');
}
$people = new People();
$people->name = 'John';
if ($people->save()) {
$info1 = new Info();
$info->title = 'info1';
$info1->save();
$info2 = new Info();
$info->title = 'info2';
$info2->save();
$people->informations()->sync(array(info1->id, info2->id));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question