Return Back does work inside Excel::load() Function: Laravel 4.2

Question

I am trying to Import an Excel File into DB, after checking File Format, which I have done successfully, the Only Part where I am getting a problem is returning back on my view page with error or success message.

My Controller::

 Excel::load(Input::file('datafile'), function ($reader) use($attr_array) {

            ...

            //IF DATA MATCH

            if($data_match == 'true')
            {                
                    ...

                    foreach ($reader->toArray() as $row) {

                        $Pdt_data->slug = Util::uniqueSlug($row['attr_name_value'], 'Product');
                        $rem_value['slug'] = $Pdt_data->slug;
                        $resultant = array_merge($rem_value, $row);

                        //INSERT INTO DATABASE
                        Product::firstOrCreate($resultant);
                    }
                    \Session::flash('success', 'Data uploaded successfully.');
                    return Redirect::back();                   
            }               
            //IF DATA DOES NOT MATCH
            else
            {                   
                \Session::flash('error', 'You Are Trying To Import File Of Different Category!');
                  return Redirect::back();
            }
        });

I had no help with Duplicate questions, Any help would be highly appreciated.


Show source
| laravel   | excel   | php   | return-value   2016-10-05 08:10 1 Answers

Answers to Return Back does work inside Excel::load() Function: Laravel 4.2 ( 1 )

  1. 2016-10-10 09:10

    I have finally solved this problem myself by :

    • Defining a variable outside Excel Function
    • Pass it to function with &
    • setting value of variable true or false on If Condition
    • Call Redirect to URL from outside Excel Function on the basis of values set earlier.

    Updated Code:

     $importstatus = ''; //DEFINING VARIABLE OUTSIDE EXCEL FUNCTION
    
    Excel::load(Input::file('datafile'), function ($reader) use($attr_array) {
    
                ...
    
                //IF DATA MATCH
    
                if($data_match == 'true')
                {                
                        ...
    
                        foreach ($reader->toArray() as $row) {
    
                            $Pdt_data->slug = Util::uniqueSlug($row['attr_name_value'], 'Product');
                            $rem_value['slug'] = $Pdt_data->slug;
                            $resultant = array_merge($rem_value, $row);
    
                            //INSERT INTO DATABASE
                            Product::firstOrCreate($resultant);
                        }
                        \Session::flash('success', 'Data uploaded successfully.');
                        $importstatus = 'fine';    //SETTING A VARIABLE VALUE              
                }               
                //IF DATA DOES NOT MATCH
                else
                {                   
                    \Session::flash('error', 'You Are Trying To Import File Of Different Category!');
                      $importstatus = 'error'; //SETTING A VARIABLE VALUE
                }
            });
    
    //CHECK VARIABLE VALUE FOR DECISION
    if ($importstatus == 'fine') {
                    \Session::flash('success', 'Data uploaded successfully.');
                    return Redirect::back();
                } else {
                    \Session::flash('error', 'You are trying to import file of Different Category!');
                    return Redirect::back();
                }
    

Leave a reply to - Return Back does work inside Excel::load() Function: Laravel 4.2

◀ Go back