lundi 3 février 2020

Select and generate insert statement from database with laravel coding

Hello there hope you will doing well. I want to select table records and then generate insert statements from that selected tables and save those insert statements in a text file how i can get this in laravel I have search alot but could not find any solution.Please help me.

Thanks in advance.

Blade code.

<form  id="tblExportsForm" autocomplete="off">
    
    <fieldset>
      <legend>Database Export Synchronization</legend>
      <div class="form-group">
        <label class="cus-label">Select Table(s) <span class="badge badge-success circle"></span></label>
        <select name="tblExportSelect[]" id="tblExportSelect" class="form-control" required multiple>
              <option value="">Select an option</option>
              @foreach($tables  as $key => $table)              
              <option value="" data-id = ""></option>            
              @endforeach
        </select>
      </div>
      <div class="form-group btn-toggle">
        <button type="submit" id="BtnSubmit" class="btn btn-primary" style="letter-spacing: 2px;"><i class="fas fa-sync-alt"></i> Export Data </button>
      </div>
    </fieldset>
    <div id="loader"></div>
  </form>

Blade Javascript

$('#BtnSubmit').on('click',function(){
      $("form#tblExportsForm").validate({
         errorElement:'span',
         errorClass:'help-block pull-left err',
         ignore:":hidden:not(select)",
         debug:true,
         rules:{
            tblExportSelect:{
               required:true
            },
         },
         highlight:function(element){
            $(element).closest('.form-group').addClass('has-error');
         },
         errorPlacement: function (error, element) {
              if(element.hasClass('select2') && element.next('.select2-container').length) {
                  error.insertAfter(element.next('.select2-container'));
                } 
                else if (element.parent('.form-group').length) {
                  error.insertAfter(element.parent());
                }
                else {
                    error.insertAfter(element);
                }
            },
         success:function(label){
            label.closest('.form-group').removeClass('has-error');label.remove();
         },
         invalidHandler:function(form,validator){
            if(!validator.numberOfInvalids())return;
         },
         messages:{

         },
         submitHandler:function(form,e){
            var btn=document.querySelector("#BtnSubmit");
            btn.style.display="none";var img=document.createElement("img");
            img.setAttribute("src",base_url+'front/images/loading.gif');
            var loader=document.querySelector("#loader");loader.appendChild(img);
            var url="<?php echo route('database.export-txtProcess');?>";
            var cur_url="<?php echo route('database.export-txt');?>";

            //var tblExportSelect  = $("#tblExportSelect").val();


            var pushArray = [];
            $.each($("#tblExportSelect option:selected"), function(){
            pushArray.push($(this).data("id"));
            });

            var data  = new FormData();

            data.append('tblExportSelect',pushArray);


            //$("#tblExportSelect").val(selected);


            axios({
                method: 'POST',
                url: url,
                data: data
              })
            .then(function(res){
              // console.log(res);
            })
         e.preventDefault();
      }
   });
   });

Controller Method

public function exportTxtProcess(Request $request){

      /*dd($request->tblExportSelect);*/

       $tables  = explode(",", $request->tblExportSelect);

       foreach ($tables as $table) {
          $output   =  DB::statement("SELECT * FROM $table");

          print_r($output);
       }
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire