1

I am getting below error:

ErrorException (E_UNKNOWN) Undefined variable: id

below is my code, It is from "Controller"

public function show($id)
 {
  if(Auth::user()) {
   $message = Message::where(function($query) {
    $query->where('from_user', '=', Auth::user()->username)->where('id', '=', $id);
   })->orWhere(function($query) {
    $query->where('from_user', '=', Auth::user()->username)->where('id', '=', $id);
   });
   if($message == null) {
    return Redirect::action('MessageController@index');
   }
   return View::make('view', ['message' => $message, 'active' => 'none']);
  }
 }

Update: My Route is : Route::get('/message/{id}', 'MessageController@show')->before('auth');

I could not understand, what i am messing with up, I have already passed $id in function argument.

user3767643
  • 674
  • 2
  • 9
  • 25

1 Answers1

1

No, you didn't.
Variable $id is passed to the function show() while you trying to use it inside lambda function passed to the where()/orWhere() method. If you wish to use inside lambda function variable from outer scope you should use use($variableName) construct.

Instead of function($query) { use function($query) use($id) {

Relate answer about use: https://stackoverflow.com/a/8403958/882813
Bear in mind variable early binding.

Community
  • 1
  • 1
user882813
  • 813
  • 5
  • 16