-1

i need to use multiple variables in my php use function. i wrote it like this.

public function show($id, $userid)
    {
        $auth = $userid;
        $user = User::findOrFail($id);
        $message = Message::where(function ($q) use ($id) {
          $q->where('from', $userid);
          $q->where('to', $id);
          $q->where('type', 0);
        })->orWhere(function ($q) use ($id){
          $q->where('from', $id);
          $q->where('to', $userid);
          $q->where('type', 1);
        })->with('user')->get();
        return response()->json([
          'message' => $message,
          'user' => $user
        ]);
    }

but it shows me a error Undefined variable: userid. how can i fix this. how can i user userid variable in use.

kalana93
  • 71
  • 3
  • 9

1 Answers1

1
public function show($id, $userid)
{
    $auth = $userid;
    $user = User::findOrFail($id);
    $message = Message::where(function ($q) use ($id,$userid) {
      $q->where('from', $userid);
      $q->where('to', $id);
      $q->where('type', 0);
    })->orWhere(function ($qq) use ($id,$userid){
      $qq->where('from', $id);
      $qq->where('to', $userid);
      $qq->where('type', 1);
    })->with('user')->get();
    return response()->json([
      'message' => $message,
      'user' => $user
    ]);
}
Nirav Bhoi
  • 559
  • 9
  • 17
  • try now if still not working dd($userid); before $message ..... – Nirav Bhoi Apr 17 '20 at 05:35
  • Code-only answers are low-value on Stackoverflow because they do a very poor job of educating/empowering the OP and thousands of future developers. If you are going to continue contributing, please craft answers with the intent to "educate" rather than merely "resolve". – mickmackusa Apr 17 '20 at 07:11
  • @mickmackusa ok – Nirav Bhoi Apr 17 '20 at 07:34