Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jenya's avatar
Level 2

How can i add a new item in one-to-many relation while updating

Hello everyone. I have 2 related models in my Laravel 5.6 app

1st is Card model l and and it has a

public function Control()
    {
        return $this->hasMany('App\Control','card_id');
    }

Control model also have a

  public function Card()
    {
        return $this->belongsTo('App\Card');
    }

in my resource controller i am going to edit given relation in such way

//CardController
public function update(Request $request, $id)
    {
     $cards= Card::find($id)->control;
     foreach ($cards as $card=>$val)
      {
          $val->work=$request->get('work')[$card];
          //editing 'work' field, i get values from 'work[]' array -it is a textarea fields from my 
          //edit.blade
          $val->save();
     }
}

but if i try to add some new textareas in my edit.blade using jquery append method, so i have for example two existing textsareas and i add another 4 textareas, how to add data from this new and old textareas properly? Thank you for attention

0 likes
4 replies
mvd's avatar

Hello @jenya ,

You could give new textarea's another field name, example 'new_textarea[]' In your update controller add another for loop

foreach($request->input('new_textarea') as $newTextarea) {
$newCard = new Card();
$newCard->control_id_here = $request->input('control_id');
$newCard->textarea_value = $newTextarea;
$newCard->save();
}
1 like
jenya's avatar
Level 2

@mvd, thank you for your post. it is a good idea to add new records like you advise but if i want to delete for example 2 of 3 existing textarea using jquery from my blade, how to save reords now?

grenadecx's avatar
Level 7

You could either store the id of the ones you delete in a separate array, and when you save, you can delete those. (probably the best option)

You could also store existing id of a card, in a hidden input associated with the message (or) embedded with the array itself. And then when you save you could just store and then delete those that is missing.

Quick example

<input type="hidden" name="newTextarea[0][id]" value="1">
<textarea name="newTextarea[0][text]"></textarea>

<input type="hidden" name="newTextarea[1][id]" value="2">
<textarea name="newTextarea[1][text]"></textarea>

And then

$card_ids = [];

foreach($request->input('newTextarea') as $newTextarea) {
    $newCard = Card::findOrNew($newTextarea['id']); // if id is empty, it creates it
    $newCard->control_id = $request->input('control_id');
    $newCard->textarea_value = $newTextarea['text'];
    $newCard->save();

    $card_ids[] = $newCard->id;
}

// Utilize whereNotIn for the card ids to delete those that we didn't find or update
Card::whereNotIn('id', $card_ids)->where('control_id', $request->input('control_id'))->delete();
1 like
jenya's avatar
Level 2

Great thanks to @GRENADECX, his advice helps me how to delete records and to @mvd for his post about adding new records! But i have heard about attach and sync methods which can used for updating related models? Are they for my one to many relation or for many to many ect..?

Please or to participate in this conversation.