Issue with Multiple Route Parameters

Published 1 year ago by Tipoff

I am working on a route that looks like:


Route::get('quotes/{quoteauthor}/{quote}', '[email protected]');

It works, but it has an issue: When I visit any quote id, it will make the page appear behind an incorrect quote author. I notice the same issue here with the discussions on Laracasts:

https://laracasts.com/discuss/channels/laravel/issue-with-multiple-route-parameters https://laracasts.com/discuss/channels/vue/issue-with-multiple-route-parameters

Both of those show the same page, but one is an incorrect URL putting this discussion behind a channel where it does not exist. Is there a way to limit the second parameter to only show those items that have a relationship with the first model? How would I redirect the URL to the correct parameter on the first model?

Best Answer (As Selected By Tipoff)
Snapey

have a read of this good article . it may steer you in the right direction

https://sebastiandedeyne.com/posts/2017/non-breaking-seo-friendly-urls-in-laravel

Snapey
Snapey
1 year ago (1,003,075 XP)

have a read of this good article . it may steer you in the right direction

https://sebastiandedeyne.com/posts/2017/non-breaking-seo-friendly-urls-in-laravel

Tipoff
Tipoff
1 year ago (26,000 XP)

Thanks, @Snapey. That worked. Here's what I used in my QuotesController.php file:


    /**
     * Display the specified resource.
     *
    * @param  $authorId
     * @param  \App\Quote  $quote
     * @return \Illuminate\Http\Response
     */
    public function show($quoteauthor, Quote $quote)
    {
        if ($quoteauthor !== $quote->author->slug) {
            return redirect()->to($quote->path());
        }

        return view('quotes.quotations.show', compact('quote'));
    }

Please sign in or create an account to participate in this conversation.