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

nob54's avatar
Level 1

How do I set the unit in the url?

Hey guys!

Well, I'm creating a Delivery using Laravel, Vue and Inertia. I've reached a point where I'm racking my brain and I can't get something.

I have an admin area and inside it I can create 'managers' and link them to a 'unit' (That's in case the snack bar has more than one unit).

What I want to do is put the name of the unit in the url, even for the customer to know which unit he is buying, as well as the admin to know which unit he is administering.

I thought to solve this using Middleware, something similar to LaravelLocalization that it sets the location...

I have a table of units and users (With roles) they are in a one to one relationship.

How can I do this? If my question is not clear, please ask for more information. Thanks!

0 likes
6 replies
Niush's avatar

You are in the right track of using middleware similar to localization. Here is the general gist:

  • Create a SetUnit Middleware:
class SetUnit
{
    public function handle(Request $request, Closure $next): Response
    {
        // if user has no unit, abort
        if (!$request->user()->unit) {
            abort(403, 'You are not allowed to access this resource.');
        }

        $requested_unit = $request->route('unit');
        $users_unit = $request->user()->unit->slug; // name

        URL::defaults(['unit' => $users_unit]); // auto-fills unit when using route()

        // if current unit is not equal to user's unit, redirect to the user's unit dashboard
        if ($requested_unit !== $users_unit) {
            return redirect()
                ->route('unit.dashboard', ['unit' => $users_unit])
                ->with('error', 'Redirected to your unit.');
        }

        return $next($request);
    }
}
  • Register your middleware in $middlewareAliases:
'set.unit' => \App\Http\Middleware\SetUnit::class,
  • And the route something like this:
Route::prefix('{unit}')
    ->name('unit.')
    ->middleware(['auth', 'set.unit'])
    ->group(function () {
        // All routes inside...
        Route::get('/dashboard', function () {
            dd(route('unit.dashboard'));
            // you can see that {unit} fragment is automatically added.
        })->name('dashboard');
    });

Hope it helps.

1 like
nob54's avatar
Level 1

@Niush

Thanks for the answer!

That way you made all users have to be linked to a unit... But that only works if my user has a unit linked.

How would I do it in a way that doesn't need it? As this project is a delivery project, users without an account will be able to enter to see the products, but as they don't have an account they cannot enter.

Only those who own a unit are the 'managers', not normal users... I was in doubt...

nob54's avatar
Level 1

@Niush Very good! I will see your code Thank's for your time

Snapey's avatar

you could put the unit on the end of the URL and make it optional.

If the controller that receives the request does not actually need the unit name then it makes no odds whether it is there or not.

So then the only bit you need to take care of is adding the unit to the end of each link.

You could have a 'current unit' for the user in session and then add it into each route when you create the href or form action.

To further illustrate, see my site. here is an example url

https://speakernet.co.uk/talk/4583/a-garden-within-doores

You will note the resource id (4583) is in the URL followed by a slug that exists in my case purely for SEO reasons. The controller never looks at that part of the URL.

1 like

Please or to participate in this conversation.