Home Calling variables to sub function?
Reply: 1

Calling variables to sub function?

Hasan Tıngır
Hasan Tıngır Published in 2017-12-05 21:53:20Z

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".


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);

    return view('frontend.single')

Any advice ?

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);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299799 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO