dimanche 11 mars 2018

Save(Insert ),Update,Delete data from dynamic form Laravel 5.*

Hi i have an dynamic form and i don't know how to update or delete it if i add a new field or if i delete a field .

I have an form with input

    <div class="row">
   <div class=""
       <textarea class="from-control"  name="somename[]" id="field"> 
       </textarea>
{!! $errors->first('somename[]',:message) !!}
</div>
    <button class="btn add-field">add</button>
    <button class="btn remove-field">remove</button>
    <div class=""
          <textarea class="from-control" name="name[]" id="field">
         </textarea>
    {!! $errors->first('name[]',:message) !!}
   </div>
</div>

<div class="row" id="newfield">
    <!-- new textarea -->
</div>

i can add a new field when i save i use foreach

 foreach ($request->somename as  $index => $value) {

    $table=  new TableName;
        $table->name= $request->name[$index];
        $table->somename= $request->root_cause[$index];

    $table->save();
    }

For updating i use:

        $id = $request->id;
 foreach ($request->somename as  $index => $value) {

        $table=  TableName::find($id);
            $table->name= $request->name[$index];
            $table->somename= $request->root_cause[$index];

        $table->save();
        }

And the big problem is:

+----------------------+------------------------+
|some input name 1     | input name 1           |
|----------------------|------------------------|
|some input nam 2      | input name 2           |
|----------------------|------------------------|
|some input name 3     | input name 3           |
+----------------------+------------------------+

If for example i edit this and i delete one field how can i update then the query so that input 3 will be removed from database and after refresh i will get?

+----------------------+------------------------+
|some input name 1     | input name 1           |
|----------------------|------------------------|
|some input nam 2      | input name 2           |
+----------------------+------------------------+

And how to update it or save if for example i change the input field or i add a new one:

+----------------------------+------------------------+
|some  name 1                | name 1                 |
|----------------------------|------------------------|
|some nam 2                  | name 2                 |
|----------------------------|------------------------|
|some name 3                 | name 3                 |
|----------------------------|------------------------|
|some  new name 4            | new name 4             |
+----------------------------+------------------------+



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire