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

manof360's avatar

pass Slug and localization to route

Hi everyone.. I working on a website with two language to show success stories .. the problem start when click read More I get an issue when I pass the param of slug to the route, this is my route in balde :

<a href="{{route('success_details',['slug'  => $story->slug,'locale' => App()->getLocale()]) }}" >Read More</a>

in the web.php :

    Route::get('success','HomPageController@success')->name('success');
            Route::get('success/{slug}','HomPageController@success_details')->name('success_details');

this is the controller

    public function success_details($slug)
{

    $story = Story::where('id', request()->slug)
        ->orWhere('slug', request()->slug)
        ->firstOrFail();
    return view('user.stories_details', compact('story'));
}

the error message :

    Facade\Ignition\Exceptions\ViewException
Missing required parameters for [Route: success_details] [URI: {locale}/success/{slug}]. (View:                  
    C:\Users\ManOf360\Desktop\success_story\resources\views\user\layouts\header.blade.php)
    http://127.0.0.1:8000/en/success/domind-in-the-stone-of-muntinte

and this is the header.blade.php :

  <li>
         <a href="{{route('success',app()->getLocale())}}">@lang('header.Success Stories')</a>
      </li>

help please :)

0 likes
51 replies
Martal's avatar

@manof360

It seems like $story->slug or app()->getLocale() returns null

Could you check it?

Snapey's avatar

how can the route helper know that locale is required? Is the route inside a route group that accepts locale ?

manof360's avatar

@martal the reslut of check by dd() ;

dd( $story->slug);   = "domind-in-the-stone-of-muntinte" 

     dd( App()->getLocale());    = "en"

so no null

manof360's avatar

@snapey yes Mr : @snapey the route inside this code :

 Route::group(['prefix' => '{locale}', 'where' => ['locale' => 'en|fa|ar'],'middleware' => 'setlocale'],function (){

 Route::get('success','HomPageController@success')->name('success');
   Route::get('success/{slug}','HomPageController@success_details')->name('success_details');    });
Martal's avatar

@manof360

Sounds crazy, but may be you should use lowercased helper function app() in blade? Like this

<a href="{{route('success_details', ['slug'  => $story->slug,'locale' => app()->getLocale()]) }}" >Read More</a> 

And, by the way, did you call route('success_details',[...]) only in that place? Do you have another calls to it?

manof360's avatar

@martal MR: @martal I change the helper function to lowercase and no change .

and yes I call route('success_details',[...]) only in this place

Martal's avatar

@manof360

Sorry, not enough data to find the reason of the error.

Could not you show a full error message?

And also, please, show php artisan route:list --path=success output.

manof360's avatar

this is the head of error

facade\Ignition\Exceptions\ViewException Missing required parameters for [Route: success_details] [URI: {locale}/success/{slug}]. (View: C:\Users\ManOf360\Desktop\BFD+Admin- zero to hero\BFD_Org\resources\views\user\layouts\header.blade.php) http://127.0.0.1:8000/en/success/domind-in-the-stone-of-muntinte

and this is the code

       <?php

  namespace Illuminate\Routing\Exceptions;

use Exception;

  class UrlGenerationException extends Exception

{

/**

 * Create a new exception for missing route parameters.

 *

 * @param  \Illuminate\Routing\Route  $route

 * @return static

 */

public static function forMissingParameters($route)

{

    return new static("Missing required parameters for [Route: {$route->getName()}] [URI: {$route->uri()}].");

}

}

manof360's avatar

@martal this the outbout :

    | Domain | Method   | URI                     | Name            | Action          | Middleware      |
+--------+----------+-------------------------+-----------------+-----------------------------+---------------+|  

  | GET|HEAD | {locale}/success/{slug} | success_details |  App\Http\Controllers\HomPageController@success_details | web,setlocale |
Martal's avatar

@manof360

Sorry for being unclear. I meant full error message in browser window.

I cannot get access to you localhost to see it myself.(=

1 like
Martal's avatar

@manof360

What if not to start a uri with a parameter?

To make something like posts/{locale}/success/{slug} instead? Will it work?

Martal's avatar

Sorry, was absent.

Did you find the error?

Did you try to change route as I had said?

manof360's avatar

till now no .!

I do not want to change the route to be as you said , because I have a lot of routs .

😊

Martal's avatar

@manof360

But you could change it temporarily to find out whether the parameter on the first position was the reason of your error or not.

Sorry, I do not have enough data to assume another reason of your error.

manof360's avatar

Hi Mr: @martal thanks a lot for your helping šŸ™‚ .. finally it's work šŸ¤— .

I change the routes to take the second parameter as optional ( {$slug?} )

like this :

  Route::get('success','HomPageController@success')->name('success');
      Route::get('success/{slug?}','HomPageController@success_details')->name('success_details');

and in the blade :

<a href="{{route('success_details',['locale' => app()->getLocale(), 'slug' => $story->slug])}}" class="read-     
   more">Read More</a>
Snapey's avatar

I don't like the fact that this works but we don't know why. Making the slug optional should have no effect.

What you have also done is altered the order of the array passed to the route helper. This should not matter but could be relevant?

1 like
Martal's avatar

@manof360

Making slug optional means that it could be null in some cases. I had said that it seemed like slug is null, but you answered that it was not.

manof360's avatar

yes Mr: @snapey even me I don't like the fact that this works but we don't know why > and really I still afraid about this routes , but what can I do

manof360's avatar

yes every time I check the slug it was not null .. and really I don't know another way for now /

Martal's avatar

@manof360

If you have enough time it is better to find out what was the reason.

May be there is a bug in laravel. You could find it out and fix. If you want of course.

Martal's avatar

@manof360

dd($slug); stops execution of a script. And error (null value in our case) could be not on the first $slug but on second (if you have some).

You can use logger('Slug is', [$slug]); to log data without stopping execution.

1 like
Snapey's avatar

Your route statement that is throwing the error, is it inside a loop?

remove the optional ? from the route in wep.php and change the route statement to use null coalesce

<a href="{{route('success_details',['locale' => app()->getLocale(), 'slug' => $story->slug ?? 'no-slug-here'])}}" class="read-more">Read More</a>

Then advise the behaviour you see

1 like
manof360's avatar

I changed it as you suggested, but This back us to the same error

manof360's avatar

dear friend : @martal .. I ma sorry I late . the output of logger('Slug is', [$slug]); is :

 [2019-12-15 11:24:12] local.DEBUG: Slug is ["domind-in-the-stone-of-muntinte"]
Maria30's avatar

You pass 2 parameters (slug and locale) to the url, and your route has only slug.

So you probably have to do this:

Route::get('success','HomPageController@success')->name('success'); Route::get('success/{slug}/{locale}','HomPageController@success_details')->name('success_details');

And also pass $locale into your success_details() method.

manof360's avatar

Hi @maracaibo , I am already hit the right data with the slug and I get the details of story from the database , the problem comes when the laravel want to show the story details in views .... when I make dd() for every line it's work fine, every thing work fine till this line :

return view('user.stories_details', compact('story'));

the error show up

manof360's avatar

@maracaibo I have prefix for the locale parameter

Route::group(['prefix' => '{locale}', 'where' => ['locale' => 'en|fr|ar'],'middleware' => 'setlocale'],function (){

 Route::get('success','HomPageController@success')->name('success');
 Route::get('success/{slug?}','HomPageController@success_details')->name('success_details');

 });
Next

Please or to participate in this conversation.