php - Saving/updating one to many relationships in Laravel -
i'm wondering best approach when saving (for first time) or updating one-to-many relationships laravel.
at simple level have person
model can have multiple appointment
models associated it. these relationships established on models themselves, need able to;
- display them when editing
person
- allow them saved in first instance
- allow them updated in future
the tricky part want them embedded within form editing person
itself, rather on separate page. so, fields appointment
s shown within expandable table, in middle of person
form.
so far i've managed send form data in way means array contains full details of appointment
models, convert appointment
models , save them. doesn't feel elegant though, , i'm thinking there better way this.
i ran across few days earlier, , figured best way modify appointments, send data, , loop between database entries , update them 1 one. ofcourse wouldn't able have single function both creating , modifying appointments looking @ possibly 2 views end user. 1 creating appointments, , 1 ammending them.
so, if data sent like:
appointment[0] appointment[1] appointment[2]
within controller, can query retrieve appointments, such like
$appointments = appointment::where('user_id','=',$userid)->get();
and loop, of each appointment update contents using foreach.
$i=0; foreach($appointments $appointment){ $appointment->date = input::get('appointment')[$i]; $appointment->save(); $i++; }
Comments
Post a Comment