Saturday, May 5, 2018

capture user id when login and save user id based on operations - development

capture user id when login and save user id based on operations

I need to capture login user and when i add question i need to save the corresponding user id in the questions table.i’m getting user id when i login but it is not saving in the question table

Controller with store function

 public function store(Request $request)
    {
        //
        $last_que = Question::orderBy('question_id', 'desc')->first();
        if ($last_que != null) {
            $old_queId = $last_que->question_id;
            $old_queId = $old_queId + 1;
        } else {
            $old_queId = 1;
        }
        $qorder=$request->input('order');
        $question=new Question();
        $quest=$question->checkDuo($qorder);
        if(count($quest)==0)
        {
            $que=Question::create([
                'question'=>$request->input('question'),
                'question_id'=>$old_queId,
                'question_type'=>$request->input('qtype'),
                'question_schedul'=>$request->input('qschedule'),
                'created_user_id'=>Session::get('created_id'),
                'order_no'=>$request->input('order')
            ]);
            if($que)
            {
                return redirect()->route('questions.index')->with('success', 'Successfully saved');
            }
        }
        else
        {
            return redirect()->back()->with('fail', 'Unable to save..! Entry with same order no. already exist');

        }
    }

in Login index file this is i used capture the user id

<?php
if (!empty($id)) {
    Session::put('created_id', $id);
}
?>

Login controller

 public function postSignIn(Request $request)
    {

        if (Auth::attempt(['username' => $request['username'], 'password' => $request['password']])) {
            $user = DB::table('users')->where([['username', '=', $request['username']], ['status', '=', '0']])->first();
            $user_id = $user->user_id;


            return redirect()->route('dashboard', $user_id)->with('message', 'State saved correctly!!!');
        } else {
            return redirect()->back();
        }


    }



from Laravel Questions and Answers https://laravelquestions.com/php/capture-user-id-when-login-and-save-user-id-based-on-operations/
via Lzo Media

No comments:

Post a Comment