dimanche 29 janvier 2017

Redirect to an error Page in Laravel 5

I create a controller that reads an excel sheet and store it in database. I need to validate the tax number, if it's not match the requirements redirect to an error page with a message I create a controller, with validateTaxnumber function, and I also create an error.blade.php in view folder.

//function - import excel sheet from the client
public function importExcel()
{
    if(Input::hasFile('import_file')){

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


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

            foreach ($data as $key => $value) {

                $tax_number = $value->taxnumber;
                $regE = '/^[1-9]([0-9]{1,10}$)/';
                $this->validateTaxnumber($regE, $tax_number);

                $name = $value->name;
                $income = $value->income;
                $location = $value->location;

                $insert[] = [

                    'taxnumber' => $tax_number,
                    'name' => $name,
                    'income' => $income,
                    'location'=> $location

                ]; //insert statement


            } //foreach loop

            if(!empty($insert)){

                DB::table('items')->insert($insert);
                dump('Insert Record successfully.');

            } //if statement

        } // if - count recodrs in excel sheet

    } // if - when you import file

    return redirect('/showdata'); //When everything is ok, will render showdata page
}

public function validateTaxnumber($regex , $value)
{
    if ( !preg_match($regex, $value) ) {
        echo 'error !';
        return redirect('error')->with('error !');
    } else {
        echo 'correct string';
    }

}

}

Please how to arrange code to make it possible. What I need validate the values of an excel sheet. If it's not validate redirect to an error page.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire