Home Calling variables to sub function?
 This question already has an answer here: Accessing outside variable using anonymous function as params 2 answers I have a post variable and I want to use it my sub function. How can I do that ? When I tried it says "Undefined Variable". Controller public function getSingle($slug){$post = Post::where('slug',$slug)->first();$related_categories_posts = Post::whereHas('categories',function ($query){$query->where('category_name', $post->categories->category_name); }) ->take(3) ->get(); return view('frontend.single') ->with('post',$post) ->with('related_categories_posts',$related_categories_posts); }  Any advice ? SamHecquet 2# SamHecquet Reply to 2017-12-05 22:01:52Z  You need to pass this variable to the function by using use as described in the docs. $related_categories_posts = Post::whereHas('categories', function ($query) use ($post){ $query->where('category_name',$post->categories->category_name); })