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

s3riouss's avatar

how should i go about this

Hello guys, i am working on a small app in laravel where a user can be in charge of more then one location, made a many to many relationship between user and location, but i don't know how to give the user the ability to change the location he is currently working on.

For example Bob is managing location 1 and location 2 he can edit the items in those locations. I would like to have a drop down list where bob can change the location that he is working on (preferably in the top area)

I don't know how go about this, where to start or what to research.

Any suggestion is highly appreciated. Thank you for your time!

0 likes
6 replies
PatrickL's avatar

It would seem that all you need to do is to pass a list of locations that the user owns to your view. Then, have a route that returns the details of the selected location, which gets called every time the user makes a selection in the drop down.

realrandyallen's avatar

You could store the location id that's he's selected to work on in the session and then use a global query scope in the appropriate models to only 'see' items for that location

session()->put('location_id', 1);

Item model

<?php

namespace App;

use App\Scopes\LocationScope;
use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new LocationScope);
    }
}

LocationScope

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class LocationScope implements Scope
{
   public function apply(Builder $builder, Model $model)
   {
         if (session('location_id')) {
            $builder->where('location_id', session('location_id'));
         }
   }
}

https://laravel.com/docs/5.7/eloquent#global-scopes

Drfraker's avatar
Drfraker
Best Answer
Level 28

You can make a view for selecting locations to manage.

// in view where user would select locations to manage
@if(auth()->user()->locations->count())
    @foreach(auth()->user()->locations as $location)
        <a href="{{route('location.show', $location)}}">{{$location->name}}</a>
    @endforeach
@endif

// LocationsController assuming you are using route model binding...
public function show(Location $location) {
    // make sure this user has permission to view the location. (requires a policy class)
    $this->authorize('view', $location);

    //return a view that show the details of the location and has a button on it to edit the location
    return view('location.show')->withLocation($location);
}

If you want to use a drop down list it gets a little more difficult but not too bad. You would replace the view logic I suggested with a form in the top area that has a select input in it. The select would be populated with options from the users related locations. When you submit the form you would parse out the location they selected and show the location based on that with similar logic to what I outlined in theLocationsController except you wouldn't use route model binding you would get it from the request.

// form to display select options
@if(auth()->user()->locations->count())
    <form method="GET" action="route('location.show')">
        <select name="location">
            @foreach(auth()->user()->locations as $location)
                <option value="{{$location->id}}">{{$location->name}}</option>
            @endforeach
        </select>
        <input type="submit" value="manage location">
    </form
@endif


// LocationsController assuming you are using route model binding...
public function show(Request $request) {
    // do some validation of the request data...

    // Find the location
    $location = Location::findOrFail($request->location); // where location is the id of the location the user selected from the form

    // make sure this user has permission to view the location. (requires a policy class)
    $this->authorize('view', $location);

    //return a view that show the details of the location and has a button on it to edit the location
    return view('location.show')->withLocation($location);
}

s3riouss's avatar

Thank you so much guys, i will probably go with the solution proposed by @drfraker seems a little bit easier to implement with what i have until now.

@realrandyallen this i didn't even think about, thank you for your time very appreciated, will also try this just to learn more about scopes

Drfraker's avatar

@s3riouss If you like the solution I provided can you mark it as the best answer? Thanks.

Please or to participate in this conversation.