Laravel - JQuery Add Form Dependencies Inserting in Database












1















I have a dynamic form and a dynamic input file button.
enter image description here



The problem is that it's not dependent when I add another form. The data is inserted into one row but what I like is each form is inserted to separated row.



For example, when I add two forms, the output in the database will all be in one row. I need the implode because of the images. The images need to be inserted in ONE ROW, not the titles and descriptions.



Controller



<?php

$this->validate($request, [
'title' => 'required',
'description' => 'required',
'fleet_image' => 'required'
]);

if ($request->has('fleet_image')) {
//Handle File Upload

$fleet = ;
foreach ($request->file('fleet_image') as $key => $file) {
$filenameWithExt = $file->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $file->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$path = $file->storeAs('public/fleet_images', $fileNameToStore);
array_push($fleet, $fileNameToStore);
}

$fileNameToStore = serialize($fleet);
} else {
$fileNameToStore = 'noimage.jpg';
}

if (count($fleet)) {
foreach ($fleet as $key => $value) {
$fleetContent = new Fleet;
$fleetContent->title = $request->title[$key];
$fleetContent->description = $request->description[$key];
$implodedFleet = implode(' , ', $fleet);
$fleetContent->fleet_image = $implodedFleet;
$fleetContent->save();

return redirect('/admin/airlineplus/fleets')->with('success', 'Content Created');
}
}

return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');


View



{!! Form::open(['action'=>'AdminFleetsController@store', 'method' => 'POST','enctype'=>'multipart/form-data']) !!}
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td>{{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}
<br>
{{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}}
<br>
<form name="add_name" id="add_name">
{{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}
</form>
{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}
</td>
<td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle add', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
</tr>
</table>
{{ Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit']) }}
</div>
{!! Form::close() !!}


JQuery/Ajax



<script>  
$(document).ready(function() {
$(document).on('click', '.add', function() {
$('#dynamic_field').append('<tr><td> {{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}} <br> {{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br> {{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }} {{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}</td><td><button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>');
});

$(document).on('click', '.addFile', function() {
var elem = '<tr><td>{{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}<button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>';
$(elem).insertAfter($(this).closest('tr'));
});

$(document).on('click', '.submit', function() {
$.ajax({
method: "POST",
data: $('#add_name').serialize(),
success: function(data) {
alert(data);
$('#add_name')[0].reset();
}
});
});


$(document).on('click', '.btn_remove', function() {
$(this).closest('tr').remove();
});
});
</script>









share|improve this question





























    1















    I have a dynamic form and a dynamic input file button.
    enter image description here



    The problem is that it's not dependent when I add another form. The data is inserted into one row but what I like is each form is inserted to separated row.



    For example, when I add two forms, the output in the database will all be in one row. I need the implode because of the images. The images need to be inserted in ONE ROW, not the titles and descriptions.



    Controller



    <?php

    $this->validate($request, [
    'title' => 'required',
    'description' => 'required',
    'fleet_image' => 'required'
    ]);

    if ($request->has('fleet_image')) {
    //Handle File Upload

    $fleet = ;
    foreach ($request->file('fleet_image') as $key => $file) {
    $filenameWithExt = $file->getClientOriginalName();
    $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
    $extension = $file->getClientOriginalExtension();
    $fileNameToStore = $filename . '_' . time() . '.' . $extension;
    $path = $file->storeAs('public/fleet_images', $fileNameToStore);
    array_push($fleet, $fileNameToStore);
    }

    $fileNameToStore = serialize($fleet);
    } else {
    $fileNameToStore = 'noimage.jpg';
    }

    if (count($fleet)) {
    foreach ($fleet as $key => $value) {
    $fleetContent = new Fleet;
    $fleetContent->title = $request->title[$key];
    $fleetContent->description = $request->description[$key];
    $implodedFleet = implode(' , ', $fleet);
    $fleetContent->fleet_image = $implodedFleet;
    $fleetContent->save();

    return redirect('/admin/airlineplus/fleets')->with('success', 'Content Created');
    }
    }

    return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');


    View



    {!! Form::open(['action'=>'AdminFleetsController@store', 'method' => 'POST','enctype'=>'multipart/form-data']) !!}
    <div class="table-responsive">
    <table class="table table-bordered" id="dynamic_field">
    <tr>
    <td>{{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}
    <br>
    {{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}}
    <br>
    <form name="add_name" id="add_name">
    {{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}
    </form>
    {{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}
    </td>
    <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle add', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
    </tr>
    </table>
    {{ Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit']) }}
    </div>
    {!! Form::close() !!}


    JQuery/Ajax



    <script>  
    $(document).ready(function() {
    $(document).on('click', '.add', function() {
    $('#dynamic_field').append('<tr><td> {{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}} <br> {{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br> {{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }} {{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}</td><td><button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>');
    });

    $(document).on('click', '.addFile', function() {
    var elem = '<tr><td>{{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}<button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>';
    $(elem).insertAfter($(this).closest('tr'));
    });

    $(document).on('click', '.submit', function() {
    $.ajax({
    method: "POST",
    data: $('#add_name').serialize(),
    success: function(data) {
    alert(data);
    $('#add_name')[0].reset();
    }
    });
    });


    $(document).on('click', '.btn_remove', function() {
    $(this).closest('tr').remove();
    });
    });
    </script>









    share|improve this question



























      1












      1








      1








      I have a dynamic form and a dynamic input file button.
      enter image description here



      The problem is that it's not dependent when I add another form. The data is inserted into one row but what I like is each form is inserted to separated row.



      For example, when I add two forms, the output in the database will all be in one row. I need the implode because of the images. The images need to be inserted in ONE ROW, not the titles and descriptions.



      Controller



      <?php

      $this->validate($request, [
      'title' => 'required',
      'description' => 'required',
      'fleet_image' => 'required'
      ]);

      if ($request->has('fleet_image')) {
      //Handle File Upload

      $fleet = ;
      foreach ($request->file('fleet_image') as $key => $file) {
      $filenameWithExt = $file->getClientOriginalName();
      $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
      $extension = $file->getClientOriginalExtension();
      $fileNameToStore = $filename . '_' . time() . '.' . $extension;
      $path = $file->storeAs('public/fleet_images', $fileNameToStore);
      array_push($fleet, $fileNameToStore);
      }

      $fileNameToStore = serialize($fleet);
      } else {
      $fileNameToStore = 'noimage.jpg';
      }

      if (count($fleet)) {
      foreach ($fleet as $key => $value) {
      $fleetContent = new Fleet;
      $fleetContent->title = $request->title[$key];
      $fleetContent->description = $request->description[$key];
      $implodedFleet = implode(' , ', $fleet);
      $fleetContent->fleet_image = $implodedFleet;
      $fleetContent->save();

      return redirect('/admin/airlineplus/fleets')->with('success', 'Content Created');
      }
      }

      return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');


      View



      {!! Form::open(['action'=>'AdminFleetsController@store', 'method' => 'POST','enctype'=>'multipart/form-data']) !!}
      <div class="table-responsive">
      <table class="table table-bordered" id="dynamic_field">
      <tr>
      <td>{{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}
      <br>
      {{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}}
      <br>
      <form name="add_name" id="add_name">
      {{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}
      </form>
      {{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}
      </td>
      <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle add', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
      </tr>
      </table>
      {{ Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit']) }}
      </div>
      {!! Form::close() !!}


      JQuery/Ajax



      <script>  
      $(document).ready(function() {
      $(document).on('click', '.add', function() {
      $('#dynamic_field').append('<tr><td> {{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}} <br> {{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br> {{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }} {{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}</td><td><button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>');
      });

      $(document).on('click', '.addFile', function() {
      var elem = '<tr><td>{{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}<button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>';
      $(elem).insertAfter($(this).closest('tr'));
      });

      $(document).on('click', '.submit', function() {
      $.ajax({
      method: "POST",
      data: $('#add_name').serialize(),
      success: function(data) {
      alert(data);
      $('#add_name')[0].reset();
      }
      });
      });


      $(document).on('click', '.btn_remove', function() {
      $(this).closest('tr').remove();
      });
      });
      </script>









      share|improve this question
















      I have a dynamic form and a dynamic input file button.
      enter image description here



      The problem is that it's not dependent when I add another form. The data is inserted into one row but what I like is each form is inserted to separated row.



      For example, when I add two forms, the output in the database will all be in one row. I need the implode because of the images. The images need to be inserted in ONE ROW, not the titles and descriptions.



      Controller



      <?php

      $this->validate($request, [
      'title' => 'required',
      'description' => 'required',
      'fleet_image' => 'required'
      ]);

      if ($request->has('fleet_image')) {
      //Handle File Upload

      $fleet = ;
      foreach ($request->file('fleet_image') as $key => $file) {
      $filenameWithExt = $file->getClientOriginalName();
      $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
      $extension = $file->getClientOriginalExtension();
      $fileNameToStore = $filename . '_' . time() . '.' . $extension;
      $path = $file->storeAs('public/fleet_images', $fileNameToStore);
      array_push($fleet, $fileNameToStore);
      }

      $fileNameToStore = serialize($fleet);
      } else {
      $fileNameToStore = 'noimage.jpg';
      }

      if (count($fleet)) {
      foreach ($fleet as $key => $value) {
      $fleetContent = new Fleet;
      $fleetContent->title = $request->title[$key];
      $fleetContent->description = $request->description[$key];
      $implodedFleet = implode(' , ', $fleet);
      $fleetContent->fleet_image = $implodedFleet;
      $fleetContent->save();

      return redirect('/admin/airlineplus/fleets')->with('success', 'Content Created');
      }
      }

      return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');


      View



      {!! Form::open(['action'=>'AdminFleetsController@store', 'method' => 'POST','enctype'=>'multipart/form-data']) !!}
      <div class="table-responsive">
      <table class="table table-bordered" id="dynamic_field">
      <tr>
      <td>{{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}
      <br>
      {{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}}
      <br>
      <form name="add_name" id="add_name">
      {{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}
      </form>
      {{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}
      </td>
      <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle add', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
      </tr>
      </table>
      {{ Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit']) }}
      </div>
      {!! Form::close() !!}


      JQuery/Ajax



      <script>  
      $(document).ready(function() {
      $(document).on('click', '.add', function() {
      $('#dynamic_field').append('<tr><td> {{Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}} <br> {{Form::textarea('description', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br> {{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }} {{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle addFile', 'id'=>'addFile','name'=>'addFile', 'style'=>'font-size:15px;']) }}</td><td><button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>');
      });

      $(document).on('click', '.addFile', function() {
      var elem = '<tr><td>{{ Form::file('fleet_image',['id'=>'exampleFormControlFile1']) }}<button type="button" name="remove" class="btn btn-danger btn_remove fa fa-minus-circle"></button></td></tr>';
      $(elem).insertAfter($(this).closest('tr'));
      });

      $(document).on('click', '.submit', function() {
      $.ajax({
      method: "POST",
      data: $('#add_name').serialize(),
      success: function(data) {
      alert(data);
      $('#add_name')[0].reset();
      }
      });
      });


      $(document).on('click', '.btn_remove', function() {
      $(this).closest('tr').remove();
      });
      });
      </script>






      jquery mysql arrays ajax laravel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 7:42









      Karl Hill

      2,58212042




      2,58212042










      asked Nov 25 '18 at 7:13









      Summer WinterSummer Winter

      14710




      14710
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465431%2flaravel-jquery-add-form-dependencies-inserting-in-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465431%2flaravel-jquery-add-form-dependencies-inserting-in-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)