how to count matched id from excel and database











up vote
0
down vote

favorite












i have an excel file and i want to upload it and save data to database.



And data will be updated based on IC Number (Identity card).



If in excel file there are 4 IC are matched with IC from the database, it will count and there will be a notification if 4 data match and update to database.



The question is how to count matched ic from excel and database?
it always show zero (0)






  if($request->hasFile('file')){
$extension = File::extension($request->file->getClientOriginalName());

if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

$path = $request->file->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();

if(!empty($data) && $data->count()){

foreach ($data as $value) {
$insert['data'] = [
'no' => $value->no,
'name' => $value->name,
'ic_no' => $value->ic_no,
'disburse_date' => $value->disburse_date,
'product_type_description' => $value->product_type_description,
'amount_release' => $value->amount_release,
'amount' => $value->amount,
'date_disburse' => $value->date_disburse,
'net_disbursement' => $value->net_disbursement,
];


if(!empty($insert))
{
$user = Auth::user()->id;

$request = new DisburseFromMbsb;

//$request->cus_id = $data->id_cus;
$request->ic_no = $value['ic_no'];
$request->name = $value['name'];
$request->disburse_date = $value['disburse_date'];
$request->product_type_description = $value['product_type_description'];
$request->amount_release = $value['amount_release'];
$request->amount = $value['amount'];
$request->date_disburse = $value['date_disburse'];
$request->net_disbursement = $value['net_disbursement'];
$request->user_id = $user;

$request->save();*/

Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',0)->update(array(
'status_upload'=>'1',
'amount_release' => $value['amount_release'],
'amount' => $value['amount'],
'date_disburse' => $value['date_disburse'],
'net_disbursement' => $value['net_disbursement'],
'disburse_date' => $value['disburse_date'],
'product_type_description' => $value['product_type_description']

));



$number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count(); // this is my code to count

}

}

return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
}





this is my code to count



$number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count();










share|improve this question


























    up vote
    0
    down vote

    favorite












    i have an excel file and i want to upload it and save data to database.



    And data will be updated based on IC Number (Identity card).



    If in excel file there are 4 IC are matched with IC from the database, it will count and there will be a notification if 4 data match and update to database.



    The question is how to count matched ic from excel and database?
    it always show zero (0)






      if($request->hasFile('file')){
    $extension = File::extension($request->file->getClientOriginalName());

    if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

    $path = $request->file->getRealPath();
    $data = Excel::load($path, function($reader) {
    })->get();

    if(!empty($data) && $data->count()){

    foreach ($data as $value) {
    $insert['data'] = [
    'no' => $value->no,
    'name' => $value->name,
    'ic_no' => $value->ic_no,
    'disburse_date' => $value->disburse_date,
    'product_type_description' => $value->product_type_description,
    'amount_release' => $value->amount_release,
    'amount' => $value->amount,
    'date_disburse' => $value->date_disburse,
    'net_disbursement' => $value->net_disbursement,
    ];


    if(!empty($insert))
    {
    $user = Auth::user()->id;

    $request = new DisburseFromMbsb;

    //$request->cus_id = $data->id_cus;
    $request->ic_no = $value['ic_no'];
    $request->name = $value['name'];
    $request->disburse_date = $value['disburse_date'];
    $request->product_type_description = $value['product_type_description'];
    $request->amount_release = $value['amount_release'];
    $request->amount = $value['amount'];
    $request->date_disburse = $value['date_disburse'];
    $request->net_disbursement = $value['net_disbursement'];
    $request->user_id = $user;

    $request->save();*/

    Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',0)->update(array(
    'status_upload'=>'1',
    'amount_release' => $value['amount_release'],
    'amount' => $value['amount'],
    'date_disburse' => $value['date_disburse'],
    'net_disbursement' => $value['net_disbursement'],
    'disburse_date' => $value['disburse_date'],
    'product_type_description' => $value['product_type_description']

    ));



    $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count(); // this is my code to count

    }

    }

    return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
    }





    this is my code to count



    $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count();










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      i have an excel file and i want to upload it and save data to database.



      And data will be updated based on IC Number (Identity card).



      If in excel file there are 4 IC are matched with IC from the database, it will count and there will be a notification if 4 data match and update to database.



      The question is how to count matched ic from excel and database?
      it always show zero (0)






        if($request->hasFile('file')){
      $extension = File::extension($request->file->getClientOriginalName());

      if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

      $path = $request->file->getRealPath();
      $data = Excel::load($path, function($reader) {
      })->get();

      if(!empty($data) && $data->count()){

      foreach ($data as $value) {
      $insert['data'] = [
      'no' => $value->no,
      'name' => $value->name,
      'ic_no' => $value->ic_no,
      'disburse_date' => $value->disburse_date,
      'product_type_description' => $value->product_type_description,
      'amount_release' => $value->amount_release,
      'amount' => $value->amount,
      'date_disburse' => $value->date_disburse,
      'net_disbursement' => $value->net_disbursement,
      ];


      if(!empty($insert))
      {
      $user = Auth::user()->id;

      $request = new DisburseFromMbsb;

      //$request->cus_id = $data->id_cus;
      $request->ic_no = $value['ic_no'];
      $request->name = $value['name'];
      $request->disburse_date = $value['disburse_date'];
      $request->product_type_description = $value['product_type_description'];
      $request->amount_release = $value['amount_release'];
      $request->amount = $value['amount'];
      $request->date_disburse = $value['date_disburse'];
      $request->net_disbursement = $value['net_disbursement'];
      $request->user_id = $user;

      $request->save();*/

      Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',0)->update(array(
      'status_upload'=>'1',
      'amount_release' => $value['amount_release'],
      'amount' => $value['amount'],
      'date_disburse' => $value['date_disburse'],
      'net_disbursement' => $value['net_disbursement'],
      'disburse_date' => $value['disburse_date'],
      'product_type_description' => $value['product_type_description']

      ));



      $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count(); // this is my code to count

      }

      }

      return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
      }





      this is my code to count



      $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count();










      share|improve this question













      i have an excel file and i want to upload it and save data to database.



      And data will be updated based on IC Number (Identity card).



      If in excel file there are 4 IC are matched with IC from the database, it will count and there will be a notification if 4 data match and update to database.



      The question is how to count matched ic from excel and database?
      it always show zero (0)






        if($request->hasFile('file')){
      $extension = File::extension($request->file->getClientOriginalName());

      if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

      $path = $request->file->getRealPath();
      $data = Excel::load($path, function($reader) {
      })->get();

      if(!empty($data) && $data->count()){

      foreach ($data as $value) {
      $insert['data'] = [
      'no' => $value->no,
      'name' => $value->name,
      'ic_no' => $value->ic_no,
      'disburse_date' => $value->disburse_date,
      'product_type_description' => $value->product_type_description,
      'amount_release' => $value->amount_release,
      'amount' => $value->amount,
      'date_disburse' => $value->date_disburse,
      'net_disbursement' => $value->net_disbursement,
      ];


      if(!empty($insert))
      {
      $user = Auth::user()->id;

      $request = new DisburseFromMbsb;

      //$request->cus_id = $data->id_cus;
      $request->ic_no = $value['ic_no'];
      $request->name = $value['name'];
      $request->disburse_date = $value['disburse_date'];
      $request->product_type_description = $value['product_type_description'];
      $request->amount_release = $value['amount_release'];
      $request->amount = $value['amount'];
      $request->date_disburse = $value['date_disburse'];
      $request->net_disbursement = $value['net_disbursement'];
      $request->user_id = $user;

      $request->save();*/

      Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',0)->update(array(
      'status_upload'=>'1',
      'amount_release' => $value['amount_release'],
      'amount' => $value['amount'],
      'date_disburse' => $value['date_disburse'],
      'net_disbursement' => $value['net_disbursement'],
      'disburse_date' => $value['disburse_date'],
      'product_type_description' => $value['product_type_description']

      ));



      $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count(); // this is my code to count

      }

      }

      return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
      }





      this is my code to count



      $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count();






        if($request->hasFile('file')){
      $extension = File::extension($request->file->getClientOriginalName());

      if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

      $path = $request->file->getRealPath();
      $data = Excel::load($path, function($reader) {
      })->get();

      if(!empty($data) && $data->count()){

      foreach ($data as $value) {
      $insert['data'] = [
      'no' => $value->no,
      'name' => $value->name,
      'ic_no' => $value->ic_no,
      'disburse_date' => $value->disburse_date,
      'product_type_description' => $value->product_type_description,
      'amount_release' => $value->amount_release,
      'amount' => $value->amount,
      'date_disburse' => $value->date_disburse,
      'net_disbursement' => $value->net_disbursement,
      ];


      if(!empty($insert))
      {
      $user = Auth::user()->id;

      $request = new DisburseFromMbsb;

      //$request->cus_id = $data->id_cus;
      $request->ic_no = $value['ic_no'];
      $request->name = $value['name'];
      $request->disburse_date = $value['disburse_date'];
      $request->product_type_description = $value['product_type_description'];
      $request->amount_release = $value['amount_release'];
      $request->amount = $value['amount'];
      $request->date_disburse = $value['date_disburse'];
      $request->net_disbursement = $value['net_disbursement'];
      $request->user_id = $user;

      $request->save();*/

      Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',0)->update(array(
      'status_upload'=>'1',
      'amount_release' => $value['amount_release'],
      'amount' => $value['amount'],
      'date_disburse' => $value['date_disburse'],
      'net_disbursement' => $value['net_disbursement'],
      'disburse_date' => $value['disburse_date'],
      'product_type_description' => $value['product_type_description']

      ));



      $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count(); // this is my code to count

      }

      }

      return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
      }





        if($request->hasFile('file')){
      $extension = File::extension($request->file->getClientOriginalName());

      if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

      $path = $request->file->getRealPath();
      $data = Excel::load($path, function($reader) {
      })->get();

      if(!empty($data) && $data->count()){

      foreach ($data as $value) {
      $insert['data'] = [
      'no' => $value->no,
      'name' => $value->name,
      'ic_no' => $value->ic_no,
      'disburse_date' => $value->disburse_date,
      'product_type_description' => $value->product_type_description,
      'amount_release' => $value->amount_release,
      'amount' => $value->amount,
      'date_disburse' => $value->date_disburse,
      'net_disbursement' => $value->net_disbursement,
      ];


      if(!empty($insert))
      {
      $user = Auth::user()->id;

      $request = new DisburseFromMbsb;

      //$request->cus_id = $data->id_cus;
      $request->ic_no = $value['ic_no'];
      $request->name = $value['name'];
      $request->disburse_date = $value['disburse_date'];
      $request->product_type_description = $value['product_type_description'];
      $request->amount_release = $value['amount_release'];
      $request->amount = $value['amount'];
      $request->date_disburse = $value['date_disburse'];
      $request->net_disbursement = $value['net_disbursement'];
      $request->user_id = $user;

      $request->save();*/

      Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',0)->update(array(
      'status_upload'=>'1',
      'amount_release' => $value['amount_release'],
      'amount' => $value['amount'],
      'date_disburse' => $value['date_disburse'],
      'net_disbursement' => $value['net_disbursement'],
      'disburse_date' => $value['disburse_date'],
      'product_type_description' => $value['product_type_description']

      ));



      $number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count(); // this is my code to count

      }

      }

      return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
      }






      php excel laravel count controller






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 2:15









      nvrzl

      418




      418
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Assuming all Code you wrote is correct (I have no idea how the Excel Package works ),
          there are a few lines that need altering and you are good to go.



          First you have to declare the variable number outside the scope of forEach
          After that, instead of



          $number = Loandisburse.......


          You have to use



          $number += Loandisburse.........


          In order not to overwrite your previous values.



          That means you have to try the following code:



          if($request->hasFile('file')){
          $extension = File::extension($request->file->getClientOriginalName());

          $number = 0;
          if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

          $path = $request->file->getRealPath();
          $data = Excel::load($path, function($reader) {
          })->get();

          if(!empty($data) && $data->count()){

          foreach ($data as $value) {
          $insert['data'] = [
          'no' => $value->no,
          'name' => $value->name,
          'ic_no' => $value->ic_no,
          'disburse_date' => $value->disburse_date,
          'product_type_description' => $value->product_type_description,
          'amount_release' => $value->amount_release,
          'amount' => $value->amount,
          'date_disburse' => $value->date_disburse,
          'net_disbursement' => $value->net_disbursement,
          ];


          if(!empty($insert)) {
          $user = Auth::user()->id;

          $request = new DisburseFromMbsb;
          $request->ic_no = $value['ic_no'];
          $request->name = $value['name'];
          $request->disburse_date = $value['disburse_date'];
          $request->product_type_description = $value['product_type_description'];
          $request->amount_release = $value['amount_release'];
          $request->amount = $value['amount'];
          $request->date_disburse = $value['date_disburse'];
          $request->net_disbursement = $value['net_disbursement'];
          $request->user_id = $user;

          $request->save();

          Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',0)->update(array(
          'status_upload'=>'1',
          'amount_release' => $value['amount_release'],
          'amount' => $value['amount'],
          'date_disburse' => $value['date_disburse'],
          'net_disbursement' => $value['net_disbursement'],
          'disburse_date' => $value['disburse_date'],
          'product_type_description' => $value['product_type_description']

          ));
          $number += Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',1)
          ->count(); // this is my code to count
          }//Closing If Not Empty
          }//Closing For Each
          }//Closing If
          }//Closing If
          return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
          }//Closing If





          share|improve this answer





















          • thank u Elie you save my day :)
            – nvrzl
            Nov 12 at 7:28










          • Did it work this way?
            – Elie
            Nov 12 at 7:32










          • yes this work perfectly, thank u so much
            – nvrzl
            Nov 12 at 7:33










          • Can you please mark the answer as correct
            – Elie
            Nov 12 at 7:35











          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',
          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%2f53255213%2fhow-to-count-matched-id-from-excel-and-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Assuming all Code you wrote is correct (I have no idea how the Excel Package works ),
          there are a few lines that need altering and you are good to go.



          First you have to declare the variable number outside the scope of forEach
          After that, instead of



          $number = Loandisburse.......


          You have to use



          $number += Loandisburse.........


          In order not to overwrite your previous values.



          That means you have to try the following code:



          if($request->hasFile('file')){
          $extension = File::extension($request->file->getClientOriginalName());

          $number = 0;
          if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

          $path = $request->file->getRealPath();
          $data = Excel::load($path, function($reader) {
          })->get();

          if(!empty($data) && $data->count()){

          foreach ($data as $value) {
          $insert['data'] = [
          'no' => $value->no,
          'name' => $value->name,
          'ic_no' => $value->ic_no,
          'disburse_date' => $value->disburse_date,
          'product_type_description' => $value->product_type_description,
          'amount_release' => $value->amount_release,
          'amount' => $value->amount,
          'date_disburse' => $value->date_disburse,
          'net_disbursement' => $value->net_disbursement,
          ];


          if(!empty($insert)) {
          $user = Auth::user()->id;

          $request = new DisburseFromMbsb;
          $request->ic_no = $value['ic_no'];
          $request->name = $value['name'];
          $request->disburse_date = $value['disburse_date'];
          $request->product_type_description = $value['product_type_description'];
          $request->amount_release = $value['amount_release'];
          $request->amount = $value['amount'];
          $request->date_disburse = $value['date_disburse'];
          $request->net_disbursement = $value['net_disbursement'];
          $request->user_id = $user;

          $request->save();

          Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',0)->update(array(
          'status_upload'=>'1',
          'amount_release' => $value['amount_release'],
          'amount' => $value['amount'],
          'date_disburse' => $value['date_disburse'],
          'net_disbursement' => $value['net_disbursement'],
          'disburse_date' => $value['disburse_date'],
          'product_type_description' => $value['product_type_description']

          ));
          $number += Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',1)
          ->count(); // this is my code to count
          }//Closing If Not Empty
          }//Closing For Each
          }//Closing If
          }//Closing If
          return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
          }//Closing If





          share|improve this answer





















          • thank u Elie you save my day :)
            – nvrzl
            Nov 12 at 7:28










          • Did it work this way?
            – Elie
            Nov 12 at 7:32










          • yes this work perfectly, thank u so much
            – nvrzl
            Nov 12 at 7:33










          • Can you please mark the answer as correct
            – Elie
            Nov 12 at 7:35















          up vote
          1
          down vote



          accepted










          Assuming all Code you wrote is correct (I have no idea how the Excel Package works ),
          there are a few lines that need altering and you are good to go.



          First you have to declare the variable number outside the scope of forEach
          After that, instead of



          $number = Loandisburse.......


          You have to use



          $number += Loandisburse.........


          In order not to overwrite your previous values.



          That means you have to try the following code:



          if($request->hasFile('file')){
          $extension = File::extension($request->file->getClientOriginalName());

          $number = 0;
          if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

          $path = $request->file->getRealPath();
          $data = Excel::load($path, function($reader) {
          })->get();

          if(!empty($data) && $data->count()){

          foreach ($data as $value) {
          $insert['data'] = [
          'no' => $value->no,
          'name' => $value->name,
          'ic_no' => $value->ic_no,
          'disburse_date' => $value->disburse_date,
          'product_type_description' => $value->product_type_description,
          'amount_release' => $value->amount_release,
          'amount' => $value->amount,
          'date_disburse' => $value->date_disburse,
          'net_disbursement' => $value->net_disbursement,
          ];


          if(!empty($insert)) {
          $user = Auth::user()->id;

          $request = new DisburseFromMbsb;
          $request->ic_no = $value['ic_no'];
          $request->name = $value['name'];
          $request->disburse_date = $value['disburse_date'];
          $request->product_type_description = $value['product_type_description'];
          $request->amount_release = $value['amount_release'];
          $request->amount = $value['amount'];
          $request->date_disburse = $value['date_disburse'];
          $request->net_disbursement = $value['net_disbursement'];
          $request->user_id = $user;

          $request->save();

          Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',0)->update(array(
          'status_upload'=>'1',
          'amount_release' => $value['amount_release'],
          'amount' => $value['amount'],
          'date_disburse' => $value['date_disburse'],
          'net_disbursement' => $value['net_disbursement'],
          'disburse_date' => $value['disburse_date'],
          'product_type_description' => $value['product_type_description']

          ));
          $number += Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',1)
          ->count(); // this is my code to count
          }//Closing If Not Empty
          }//Closing For Each
          }//Closing If
          }//Closing If
          return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
          }//Closing If





          share|improve this answer





















          • thank u Elie you save my day :)
            – nvrzl
            Nov 12 at 7:28










          • Did it work this way?
            – Elie
            Nov 12 at 7:32










          • yes this work perfectly, thank u so much
            – nvrzl
            Nov 12 at 7:33










          • Can you please mark the answer as correct
            – Elie
            Nov 12 at 7:35













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Assuming all Code you wrote is correct (I have no idea how the Excel Package works ),
          there are a few lines that need altering and you are good to go.



          First you have to declare the variable number outside the scope of forEach
          After that, instead of



          $number = Loandisburse.......


          You have to use



          $number += Loandisburse.........


          In order not to overwrite your previous values.



          That means you have to try the following code:



          if($request->hasFile('file')){
          $extension = File::extension($request->file->getClientOriginalName());

          $number = 0;
          if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

          $path = $request->file->getRealPath();
          $data = Excel::load($path, function($reader) {
          })->get();

          if(!empty($data) && $data->count()){

          foreach ($data as $value) {
          $insert['data'] = [
          'no' => $value->no,
          'name' => $value->name,
          'ic_no' => $value->ic_no,
          'disburse_date' => $value->disburse_date,
          'product_type_description' => $value->product_type_description,
          'amount_release' => $value->amount_release,
          'amount' => $value->amount,
          'date_disburse' => $value->date_disburse,
          'net_disbursement' => $value->net_disbursement,
          ];


          if(!empty($insert)) {
          $user = Auth::user()->id;

          $request = new DisburseFromMbsb;
          $request->ic_no = $value['ic_no'];
          $request->name = $value['name'];
          $request->disburse_date = $value['disburse_date'];
          $request->product_type_description = $value['product_type_description'];
          $request->amount_release = $value['amount_release'];
          $request->amount = $value['amount'];
          $request->date_disburse = $value['date_disburse'];
          $request->net_disbursement = $value['net_disbursement'];
          $request->user_id = $user;

          $request->save();

          Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',0)->update(array(
          'status_upload'=>'1',
          'amount_release' => $value['amount_release'],
          'amount' => $value['amount'],
          'date_disburse' => $value['date_disburse'],
          'net_disbursement' => $value['net_disbursement'],
          'disburse_date' => $value['disburse_date'],
          'product_type_description' => $value['product_type_description']

          ));
          $number += Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',1)
          ->count(); // this is my code to count
          }//Closing If Not Empty
          }//Closing For Each
          }//Closing If
          }//Closing If
          return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
          }//Closing If





          share|improve this answer












          Assuming all Code you wrote is correct (I have no idea how the Excel Package works ),
          there are a few lines that need altering and you are good to go.



          First you have to declare the variable number outside the scope of forEach
          After that, instead of



          $number = Loandisburse.......


          You have to use



          $number += Loandisburse.........


          In order not to overwrite your previous values.



          That means you have to try the following code:



          if($request->hasFile('file')){
          $extension = File::extension($request->file->getClientOriginalName());

          $number = 0;
          if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {

          $path = $request->file->getRealPath();
          $data = Excel::load($path, function($reader) {
          })->get();

          if(!empty($data) && $data->count()){

          foreach ($data as $value) {
          $insert['data'] = [
          'no' => $value->no,
          'name' => $value->name,
          'ic_no' => $value->ic_no,
          'disburse_date' => $value->disburse_date,
          'product_type_description' => $value->product_type_description,
          'amount_release' => $value->amount_release,
          'amount' => $value->amount,
          'date_disburse' => $value->date_disburse,
          'net_disbursement' => $value->net_disbursement,
          ];


          if(!empty($insert)) {
          $user = Auth::user()->id;

          $request = new DisburseFromMbsb;
          $request->ic_no = $value['ic_no'];
          $request->name = $value['name'];
          $request->disburse_date = $value['disburse_date'];
          $request->product_type_description = $value['product_type_description'];
          $request->amount_release = $value['amount_release'];
          $request->amount = $value['amount'];
          $request->date_disburse = $value['date_disburse'];
          $request->net_disbursement = $value['net_disbursement'];
          $request->user_id = $user;

          $request->save();

          Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',0)->update(array(
          'status_upload'=>'1',
          'amount_release' => $value['amount_release'],
          'amount' => $value['amount'],
          'date_disburse' => $value['date_disburse'],
          'net_disbursement' => $value['net_disbursement'],
          'disburse_date' => $value['disburse_date'],
          'product_type_description' => $value['product_type_description']

          ));
          $number += Loandisburse::where('ic', $value['ic_no'])
          ->where('stage','W11')
          ->where('status_upload',1)
          ->count(); // this is my code to count
          }//Closing If Not Empty
          }//Closing For Each
          }//Closing If
          }//Closing If
          return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
          }//Closing If






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 7:18









          Elie

          24217




          24217












          • thank u Elie you save my day :)
            – nvrzl
            Nov 12 at 7:28










          • Did it work this way?
            – Elie
            Nov 12 at 7:32










          • yes this work perfectly, thank u so much
            – nvrzl
            Nov 12 at 7:33










          • Can you please mark the answer as correct
            – Elie
            Nov 12 at 7:35


















          • thank u Elie you save my day :)
            – nvrzl
            Nov 12 at 7:28










          • Did it work this way?
            – Elie
            Nov 12 at 7:32










          • yes this work perfectly, thank u so much
            – nvrzl
            Nov 12 at 7:33










          • Can you please mark the answer as correct
            – Elie
            Nov 12 at 7:35
















          thank u Elie you save my day :)
          – nvrzl
          Nov 12 at 7:28




          thank u Elie you save my day :)
          – nvrzl
          Nov 12 at 7:28












          Did it work this way?
          – Elie
          Nov 12 at 7:32




          Did it work this way?
          – Elie
          Nov 12 at 7:32












          yes this work perfectly, thank u so much
          – nvrzl
          Nov 12 at 7:33




          yes this work perfectly, thank u so much
          – nvrzl
          Nov 12 at 7:33












          Can you please mark the answer as correct
          – Elie
          Nov 12 at 7:35




          Can you please mark the answer as correct
          – Elie
          Nov 12 at 7:35


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53255213%2fhow-to-count-matched-id-from-excel-and-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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python