How can we allow the funtion to have access to slug

0 votes

The following obviously results in undefined variable.

public function show($locale, $slug)
{
 $article = Article::whereHas('translations', function ($query) {
 $query->where('locale', 'en')
  ->where('slug', $slug);
 })->first();

   return $article;
}

Trying to supply the function with the $slug variable:

public function show($locale, $slug)
{
    $article = Article::whereHas('translations', function ($query, $slug) {
        $query->where('locale', 'en')
        ->where('slug', $slug);
    })->first();

    return $article;
}

results in

Missing argument 2 for App\Http\Controllers\ArticlesController::App\Http\Controllers\{closure}()

how can you allow the funtion to have access to $slug? 

Sep 30, 2020 in Laravel by kartik
• 37,510 points
74 views

1 answer to this question.

0 votes

Hello @kartik,

You have to use use to pass variables (in your case, $slug) into the closure (this is called variable inheriting):

public function show($locale, $slug)
{
      $article = Article::whereHas('translations', function ($query) use ($slug) {
        $query->where('locale', 'en') //                             ^^^ HERE
              ->where('slug', $slug);
    })->first();

    return $article;
}

If you, in the future, want to pass $locale in along with it, just comma-separate it:

Article::whereHas('translations', function ($query) use ($slug, $locale) { /* ... */ });

Hope it helps!!

Thank You!!

answered Sep 30, 2020 by Niroj
• 82,720 points

Related Questions In Laravel

0 votes
1 answer

What do you mean by Rate Limiting?How can we access control in number of routes in an application ?

Hey kartik, Laravel includes a middleware to rate ...READ MORE

answered Mar 26, 2020 in Laravel by Niroj
• 82,720 points
139 views
0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2, 2020 in Laravel by Niroj
• 82,720 points
6,334 views
0 votes
1 answer

How to access url for the current if statement of laravel?

Hello @ subham , If you want to access the ...READ MORE

answered Aug 7, 2020 in Laravel by Niroj
• 82,720 points
217 views
0 votes
1 answer

How can I serve a single HTML page from the Laravel public folder without having to use the .html extension?

Hello @kartik, You may rename the test folder ...READ MORE

answered Nov 12, 2020 in Laravel by Niroj
• 82,720 points
507 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
7,121 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
814 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
632 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
7,545 views
0 votes
1 answer

How can we use the custom table in Laravel?

Hey, We can easily use custom table in ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,720 points
481 views
0 votes
1 answer

How can we check the logged-in user info in Laravel?

Hey @kartik, yes we can keep track of ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,720 points
180 views