0

I am try to learn how to use this to post properly. I am just starting on Laravel.... recently

Why this is NOT Working?

 public function getFunctionFromUserGroup(Request $request)
    {
        try
        {
            $abc = $request->get('usertypeid');
            return response() -> json(SystemFunction::whereNotIn('Function_ID', function($q){
                    $q->select('Function_ID')->from('TB_UserAccess')->Where('UserType_ID', $abc );})->get(), 200);

        }
        catch (\Exception $e)
        {
            return response(['error'=> $e->getMessage()], 422);
        }
    }

And Why this is working?

 public function getFunctionFromUserGroup(Request $request)
    {
        try
        {
            return response() -> json(SystemFunction::whereNotIn('Function_ID', function($q){
                    $q->select('Function_ID')->from('TB_UserAccess')->Where('UserType_ID', 1);})->get(), 200);

        }
        catch (\Exception $e)
        {
            return response(['error'=> $e->getMessage()], 422);
        }
    }
1
  • difference is ` $abc = $request->get('usertypeid');` in the second one you are not passing this variable to where clause Commented Mar 12, 2018 at 21:11

2 Answers 2

2

You have to make $abc available inside the closure:

... function($q) use($abc) { ...
Sign up to request clarification or add additional context in comments.

Comments

0

To make your code run properly you need to use $abc inside your function using use() try below code

 public function getFunctionFromUserGroup(Request $request)
    {
        try
        {
            $abc = $request->get('usertypeid');
            return response() -> 
                 json (SystemFunction::whereNotIn('Function_ID', 
                 function ($q){
                    $q->select('Function_ID')
                        ->from('TB_UserAccess')
                        ->Where('UserType_ID', $abc );})->get(), 200);
        }
        catch (\Exception $e)
        {
            return response(['error'=> $e->getMessage()], 422);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.