Laravel undefined variable inside function

I get my $id from the url, but I could not find a way to use it in a function. How can I pass $ id to the request function?

I tried global $ id, but I still can't get the value, just a blank page.

My controller.

 class BookController extends Controller { public function bookIndex($id, $slug) { // echo $id works without a problem $findcover = Thing::with(array('cover' => function($query) { $query->where('imageable_type', 'App\Models\Thing') ->where('imageable_id', $id); }))->find($id); $maincover = $findcover->cover; } 

Error:

 ErrorException in BookController.php line 49: Undefined variable: id 

Line 49

 ->where('imageable_id', $id); 
+6
source share
1 answer
 class BookController extends Controller { public function bookIndex($id, $slug) { // echo $id works without a problem $findcover = Thing::with(array('cover' => function($query) use ($id) { $query->where('imageable_type', 'App\Models\Thing') ->where('imageable_id', $id); }))->find($id); $maincover = $findcover->cover; } 

Add a reserved word to use to insert $id for your query.

+10
source

All Articles