-1

I am trying to define a global variable to use in multiple Laravel routes. However, I am getting "undefined variable $title" as error message. What am I doing wrong? The code on the bottom, throws a similar error.

$title = "YAAA";
Route::get('/insertORM', function(){
    $a = $title;
});

This piece of code exists in a standard laravel application inside the routes/web.php file.

enter image description here

Johnnybossboy
  • 191
  • 3
  • 18

1 Answers1

1

The function needs to inherit the variable from the parent scope by using use()

Route::get('/insertORM', function() use ($title, $body) {
    $a = $title;
    $b = $body;
});
N69S
  • 16,110
  • 3
  • 22
  • 36