Ad

Laravel Routes Continue Url Slug

I don't know how to make 1 route display url/something, and the next route display url/something/anotherthing

Right now except the third route everything works fine. It displays the url/company_name and it shows a view with another list of links. I want when I click one link to pass $table_name, $company_name to the next view but I also want the url to be url/company_name/table_name.

I have this in my home.blade that display some links:

@foreach($companies as  $data)                   
<a target="_blank" rel="nofollow noreferrer" href="{{ route('show_tables',$company_name = $data->name )}}" class="btn btn-primary" >
{{$data->name }}
</a>
@endforeach

When I click the link in controller I have this:

   public function index($company_name) {
        $tables = DB::connection($company_name)->table('tables')->get();
        return view('apps.welcome', compact('tables', 'company_name'));
    } 

and this is my apps.welcome.blade view:

        <div class="card-body">
            @csrf
            @foreach($tables as  $data)                   
            <a target="_blank" rel="nofollow noreferrer" href="{{ route('choose_table',$table_name = $data->name)}}" class="btn btn-primary" >
                {{$data->name }}
            </a>
            @endforeach

And this are my web routes :

Route::get('/home', '[email protected]')->name('home');
Route::get('/{company_name}', 'Apps\[email protected]')->name('show_tables');
Route::get('{company_name}/{table_name}', 'Apps\[email protected]')->name('choose_table');

I'm getting this error:

Missing required parameters for [Route: choose_table] [URI: {company_name}/{table_name}]. (View: apps.welcome.blade)
Ad

Answer

You are using choose_table route in app.welcome.blade.php with one parameter missing:

<a target="_blank" rel="nofollow noreferrer" href="{{ route('choose_table',$table_name = $data->name)}}" class="btn btn-primary" >
    {{$data->name }}
</a>

Your route actually requires 2 parameters i.e. company_name & table_name:

Route::get('{company_name}/{table_name}', 'Apps\[email protected]')->name('choose_table');

Use both parameters in your route (as mentioned by @rkj in comment):

route('choose_table', ['company_name' => $company_name, 'table_name' => $data->name]);

Otherwise, make your 2nd parameter as optional:

Route::get('{company_name}/{table_name?}', 'Apps\[email protected]')->name('choose_table');

but if you are doing this, you must give default value for optional parameter in controller.

Optional Parameters

Ad
source: stackoverflow.com
Ad