Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CamKem's avatar
Level 10

Injecting a Variable into Ziggy Route

Hello, I am hoping someone can help me.

I am wanting to inject a variable into a ziggy route so that it can be used by my controller function.

The variable comes from the page props passed over by the model in the back end.

My controller looks like this

class ArticlesController extends Controller
{
    public function index() {
        $articles = Article::orderBy('created_at', 'desc')->paginate(10);
        return Inertia::render('Frontend/Articles/Index', compact('articles'));
    }

    public function show($slug)
    {
        $article = Article::where('slug', $slug)->firstOrFail();
        $can_update = Auth::check() ? Auth::user()->can('update', $article) : false;
        $can_delete = Auth::check() ? Auth::user()->can('delete', $article) : false;
        return Inertia::render('Frontend/Articles/Show', compact('article', 'can_update', 'can_delete'));
    }

}

My routes look like this

use App\Http\Controllers\Frontend\ArticlesController as FrontendArticlesController;
Route::get('/articles', [FrontendArticlesController::class, 'index'])->name('articles');
Route::get('/articles/{article:slug}', [FrontendArticlesController::class, 'show'])->name('article.show');

My front end ziggy calling looks like this (its inside a v-for statement to display the articles)

<template
       v-for="(article, key) in articles.data"
       key="article.id"
>
<link
       :href="route('article.show', [article, article.slug])"
>
{{ article.title }}
</link>
</template>

Hopefully someone can assist me with this, thanks in adavnce!!!

0 likes
6 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

needs to be an object

route('article.show', [article, article.slug])
//fixed 
route('article.show', {article: article.slug})
CamKem's avatar
Level 10

@Sinnbeck It was a typo on my end, it's just an issue i'm having with the routes now.

CamKem's avatar
Level 10

Also, I changed my controller to the following, seemed like a more concise way of coding it.

Route::controller(FrontendArticlesController::class)->group(function () {
    Route::get('/articles', 'index')->name('articles');
    Route::get('/articles/{article:slug}', 'show')->name('articles.show');
    Route::post('/articles', 'store')->name('articles.store');
});
CamKem's avatar
Level 10
<Link
       :href="route('articles.show', { article: article.slug })"
>
       <h5 class="text-gray-900 text-xl font-medium mb-2">
                {{ article.title }}
       </h5>
</Link>

However now i am getting a console error that the route does not exist.

Please or to participate in this conversation.