Shoaib Hassan Shoaib Hassan - 1 month ago 7
PHP Question

Session not destroying in laravel

I have a problem n my session. I dont destroying my session. In my session i put value of my current_url so that if I register from booking page I REDIRECT BACK TO THE PAGE AFTER REGISTRATION. But if i register from the home page it also redirect back to the booking page. My controller is

public function helpPeopleStore (Request $request) {
if (Auth::check()) {
$current_url = Session::get('current_url');
$user = Auth::user();
$industry = $request->input('industry');
if (empty($industry)) {
Session::flash('error', 'Industry is needed');
return redirect()->back()->withInput();
}
$degrees = $request->input('degrees');
if (empty($degrees)) {
Session::flash('error', 'Degree is needed');
return redirect()->back()->withInput();
}
$area_of_experiences = $request->input('area_of_experiences');
if (empty($area_of_experiences)) {
Session::flash('error', 'area of experiences is needed');
return redirect()->back()->withInput();
}

$user_industry = UserIndustry::where('user_id', $user->id)->first();
if (!empty($user_industry)) {
$user_industry = $user_industry;
} else {
$user_industry = new UserIndustry();
}

$user_degrees_exists = UserDegree::where('user_id', $user->id)->get();
if (!empty($user_degrees_exists)) {
foreach ($user_degrees_exists as $user_degree_exist) {
UserDegree::destroy($user_degree_exist->id);
}
}

$user_area_of_experiences_exists = UserAreaOfExperience::where('user_id', $user->id)->get();
if (!empty($user_area_of_experiences_exists)) {
foreach ($user_area_of_experiences_exists as $user_area_of_experience_exist) {
UserAreaOfExperience::destroy($user_area_of_experience_exist->id);
}
}

$user_industry->user_id = $user->id;
$user_industry->industry_id = $industry;
$user_industry->save();

if (!empty($degrees)) {
foreach ($degrees as $degree) {
$user_degrees = new UserDegree();
$user_degrees->user_id = $user->id;
$user_degrees->degree_id = $degree;
$user_degrees->save();
}
}

if (!empty($area_of_experiences)) {
foreach ($area_of_experiences as $area_of_experience) {
$user_area_of_experiences = new UserAreaOfExperience();
$user_area_of_experiences->user_id = $user->id;
$user_area_of_experiences->area_of_experience_id = $area_of_experience;
$user_area_of_experiences->save();
}
}
return redirect($current_url);
Session::flush();
} else {
return redirect('user/registration');
}

Session::flush();
}


Please guys help me solving this

Answer

The answer will be

public function helpPeopleStore (Request $request) {
        if (Auth::check()) {
            $current_url = Session::get('current_url');
            $user = Auth::user();
            $industry = $request->input('industry');
            if (empty($industry)) {
                Session::flash('error', 'Industry is needed');
                return redirect()->back()->withInput();
            }
            $degrees = $request->input('degrees');
            if (empty($degrees)) {
                Session::flash('error', 'Degree is needed');
                return redirect()->back()->withInput();
            }
            $area_of_experiences = $request->input('area_of_experiences');
            if (empty($area_of_experiences)) {
                Session::flash('error', 'area of experiences is needed');
                return redirect()->back()->withInput();
            }

            $user_industry = UserIndustry::where('user_id', $user->id)->first();
            if (!empty($user_industry)) {
                $user_industry = $user_industry;
            } else {
                $user_industry = new UserIndustry();
            }

            $user_degrees_exists = UserDegree::where('user_id', $user->id)->get();
            if (!empty($user_degrees_exists)) {
                foreach ($user_degrees_exists as $user_degree_exist) {
                    UserDegree::destroy($user_degree_exist->id);
                }
            }

            $user_area_of_experiences_exists = UserAreaOfExperience::where('user_id', $user->id)->get();
            if (!empty($user_area_of_experiences_exists)) {
                foreach ($user_area_of_experiences_exists as $user_area_of_experience_exist) {
                    UserAreaOfExperience::destroy($user_area_of_experience_exist->id);
                }
            }

            $user_industry->user_id = $user->id;
            $user_industry->industry_id = $industry;
            $user_industry->save();

            if (!empty($degrees)) {
                foreach ($degrees as $degree) {
                    $user_degrees = new UserDegree();
                    $user_degrees->user_id = $user->id;
                    $user_degrees->degree_id = $degree;
                    $user_degrees->save();
                }
            }

            if (!empty($area_of_experiences)) {
                foreach ($area_of_experiences as $area_of_experience) {
                    $user_area_of_experiences = new UserAreaOfExperience();
                    $user_area_of_experiences->user_id = $user->id;
                    $user_area_of_experiences->area_of_experience_id = $area_of_experience;
                    $user_area_of_experiences->save();
                }
            }
            return redirect($current_url);
            Session::flush();
        } else {
            return redirect('user/registration');
        }

        Session::flush();
    }