Thursday, March 9, 2017

redirecting from controller action to another in laravel

i was using laravel to develop a simple website i was facing undifinde variable problem and after long search i figure the problem in my controller lets see the code

public function login(Request $req){// login main
    $data= Input :: except(array('_token'));


$rule=array(
  'username'=>'required',
  'password'=>'required',
        );

$massage=array(
  'username.required'=> 'you must enter username',                                                          // message if the user name or pass kept empty
  'password.required'=> 'you must enter password',
        );


$valid=Validator::make ($data,$rule,$massage);
        if ($valid->fails()){// if ends in line 38
          return Redirect::to('login')->withErrors($valid);


        }/*for if line 34*/ else{//ends in line 162
        $username=$req->input('username');
        $password=$req->input('password');


        $checkLogin = DB::table('logfile')->where(['user_name'=>$username,'password'=>$password])->get();

        $users = DB::table('logfile')
                        ->join('student', 'logfile.id', '=', 'student.std_id')
                        ->where('user_name', $username)
                        ->first();

                                                              //written by Ahmed...................


            // this code done by ahmed to rote the student for agreement or exception
         Session::put('key', $users);

                if(count($checkLogin) > 0){//ends at line 159
                    $type=DB::table('logfile')->where('user_name', $username)->value('user_kind');
                        if($type === 2){// ends in line 61
                            return Redirect::to('doc'); 

                        }/* start at line 58*/else if($type === 3){// ends at line 70
                    $super=DB::table('logfile')
                  ->join('supervisor','logfile.id','=','supervisor.sup_id')
                  ->where('user_name',$username)
                  ->first();

                  return view('supervisor', ['super' =>$super]);         //printing name for Supervisor
                    Session::put('name',$super);        

                        }/* start in line 61*/else  if($type === 1){//ends at line 148 

                       $page=$users->std_flag;
                       $pageupdate = $page + 1;

                       if($page === 0){// ends at line 93

                      $hour=DB::table('student')->where('std_fname', $username)->value('std_hour');

                      if ($hour >= 87) {//ends at line 88                                                                      // first check the student hours 
                                                                                                 // if greater than or equal 87 go to agreement page
                        return view('agreement', ['users' =>$users]);
                        //من بعد هذي النقطة تبدا المشكلة
                      }//start at line 84
                       else {//ends at line 92

                         return view('exception', ['users' =>$users]);              // if else than 87 go to exception page
                       }//start at line  89
                       }// start at line 75

                       elseif($page === 1){

                           return view('orgselect');
                       }
                       /*elseif($page === 2){


                           return Redirect::to('printletter');
                       }
                       elseif($page === 3){

                           DB::table('student')
                                ->where('std_id', $users->std_id)
                                ->update(['std_flag' => $pageupdate]);
                           return Redirect::to('orgselect');
                       }
                       elseif($page === 4){

                           DB::table('student')
                                ->where('std_id', $users->std_id)
                                ->update(['std_flag' => $pageupdate]);
                           return Redirect::to('orgselect');
                       }
                       elseif($page === 5){

                           DB::table('student')
                                ->where('std_id', $users->std_id)
                                ->update(['std_flag' => $pageupdate]);
                           return Redirect::to('orgselect');
                       }
                       elseif($page === 6){

                           DB::table('student')
                                ->where('std_id', $users->std_id)
                                ->update(['std_flag' => $pageupdate]);
                           return Redirect::to('orgselect');
                       }
                       elseif($page === 7){

                           DB::table('student')
                                ->where('std_id', $users->std_id)
                                ->update(['std_flag' => $pageupdate]);
                           return Redirect::to('orgselect');
                       }
                       elseif($page === 8){

                           DB::table('student')
                                ->where('std_id', $users->std_id)
                                ->update(['std_flag' => $pageupdate]);
                           return Redirect::to('orgselect');
                       }*/

                }/* start at line 70*/else {

           //return Redirect::to('login')->withErrors('alert-success','You are now logged in');     
           $errors = new MessageBag(['password' => ['Email and/or password invalid.']]);             // to check login and print message Email and/or password invalid.
            return Redirect::back()->withErrors($errors)->withInput(Input::except('password'));     // to check login and print message Email and/or password invalid.

          // return Redirect::to('agree')->withErrors($valid);
         }



        }//start in line 56


}// start in line 38

}//end login main this is was the first action..

now i want if this condition is ture

if ($hour >= 87) {//ends at line 88                                                                      // first check the student hours 
                                                                                                 // if greater than or equal 87 go to agreement page
                        return view('agreement', ['users' =>$users]);

to go to the action

public function agreement(Request $request)

{

   $sestest=Session::get('key');
   $sestest1 = DB::table('logfile')
                        ->join('student', 'logfile.id', '=', 'student.std_id')
                        ->where('user_name', $sestest->user_name)
                        ->first();

   $page=$sestest1->std_flag;

   if($page!= 1){
   $pageupdate = $page + 1;

   $agr=$request->input('checkbox');

  DB::table('AgreementTerms')->insert(
  ['agreement_id' => 3, 'std_id' => $sestest->std_id, 'satus' => $agr]);

DB::table('student')
    ->where('std_id', $sestest->std_id)
    ->update(['std_flag' => $pageupdate]);
   }

   $query = DB::table('Department_city')->select('city','Dep_location')->first();



return view('orgselect', ['query' =>$query], ['sestest1' =>$sestest1]);

}

this is the route

Route::post('/agreement_check','MyController@agreement');

i really search alote but nothing works my laravel version is 5.2 hope you guys help me in this



via Adel

Advertisement