Question: form details are stored into the database but will make an automatic logout laravel 5.3

Question

form details are stored into the database but will make an automatic logout laravel 5.3

Answers 1
Added at 2017-01-02 08:01
Tags
Question

I created a FORM to add employee using laravel 5.3. when I fill the details and submit form ,all the data are inserted into the database. but after save values automatically logout. I can't find any solution for that issue.Please help me to fix this problem.this is ajax call result

form-validation.js

submitHandler: function(form) {

    var loading_el = $('#AddEmp');
    setLoader(loading_el, true);
    var userLevelId = $('#AddUserLevel').find('option:selected').attr('data-id');
    var policyId = $('#AddPolicies').find('option:selected').attr('data-id');

    $.ajax({
            url:'/addEmployee',
            dataType: "json",
            type: 'POST',
            data: {
                    email: $("#AddEmail").val(),
                    password: $("#AddEmpPassword").val(),
                    confirmPassword: $("#AddEmpConfirmPassword").val(),
                    EmpNo01: $("#AddEmpNo01").val(),
                    EmpNo02: $("#AddEmpNo02").val(),
                    UserLevel:userLevelId ,                 
                    Designation:$("#Designation").val(),    
                    Policies: policyId,      
                    FirstName: $("#AddFname").val(),
                    LastName: $("#AddLname").val(),
                    telephone: $("#AddTp").val(),
                    Mobile: $("#AddMno").val(),
                    JoinDate: $("#AddJoinDate").val(),
                    dob: $("#Adddob").val()

                },

web.php

   Route::group(['middleware'=>['admin']],function(){ // start admin routes
    Route::post('/addEmployee','web\EmployeeController@AddEmployeeDetails');
   }

EmployeeController.php

class EmployeeController extends Controller{

private $Employee;
private $policies;
private $Role;
private $Users;
private $EmployeeLeavePolicy;

public function __construct(EmployeeRepository $Employee ,LeavePolicyRepository $policies, RoleRepository $Role, UserRepository $Users,EmployeeLeavePolicyRepository $EmployeeLeavePolicy)
{
    $this->Employee = $Employee;
    $this->policies = $policies;
    $this->Role = $Role;
    $this->Users = $Users;
    $this->EmployeeLeavePolicy = $EmployeeLeavePolicy;

}

public function AddEmployeeDetails(addEmployeeRequest $request)
{
    try
    {
       $dataUser=[    //this data save in users table
            'name'=>$request['FirstName'],
            'email'=>$request['email'],
            'user_type'=>$request['UserLevel'],
            'password' => Hash::make($request['password']),

        ];

         $responseUser=$this->Users->create($dataUser);   //this is working one
        $res=$responseUser->id;

        if($responseUser)
        {
            $dataEmployee=[
                       'email'=>$request['email'],
                       'company_id'=>1,
                       'userId'=>$res,
                       'user_level'=>$request['UserLevel'],
                       'designation'=>$request['Designation'],
                       'empNo1'=>$request['EmpNo01'],
                       'empNo2'=>$request['EmpNo02'],
                       'password' => Hash::make($request['password']),
                       'first_name'=>$request['FirstName'],
                       'last_name'=>$request['LastName'],
                       'telephone'=>$request['telephone'],
                       'mobile'=>$request['Mobile'],
                       'join_date'=>$request['JoinDate'],
                       'dob'=>$request['dob'],
                       'status'=>1,
                ];

            $responseEmployee=$this->Employee->create($dataEmployee);

            if($responseEmployee)
            {
                $employeeId=$responseEmployee->id;  //get employee id
                $leaveId=$request['Policies'];      //get leave policy id

                $dataEmpLeavePolicy=[
                    'employee_Id'=>$employeeId,
                    'leave_policy_id'=>$leaveId,
                ];

                $responseLeavePolicy=$this->EmployeeLeavePolicy->create($dataEmpLeavePolicy);


            }

        Config::post('messages.ADD_USER_SUCCESS')];
            return ['success'=>'true','message'=> 'Data Added'];
        }
        else
        {
            return ['success'=>'false', 'error'=>Config::get('messages.ADD_USER_ERROR')];
        }
    }
    catch (Exception $ex)
    {
        return ['success'=>'false', 'error'=>  $ex->getMessage()];
    }

Employee.php

  class Employee extends Model{

         protected $fillable=[
                              'userId',
                              'company_id',
                              'empNo1',
                              'empNo2',
                              'user_level',       
                              'designation',        
                              'first_name',
                              'last_name',
                              'email',
                              'password',
                              'telephone',
                              'mobile',
                              'join_date',
                              'dob',
                              'status'
                           ];


     }
Answers to

form details are stored into the database but will make an automatic logout laravel 5.3

nr: #1 dodano: 2017-01-03 06:01

It's strange that it logs you out, but instead of returning an array on success/failure (since you are making an AJAX call), return a response with json instead.

return response()->json(['success'=>'true','message'=> 'Data Added']);

Source Show
◀ Wstecz