I want to edit a record by its id and I have the following code in my view:
<a href="{{route('guest.edit',$g->id)}}" class="btn btn-simple btn-warning btn-icon edit"><i class="ti-pencil-alt"></i></a>
And so I have in my route:
Route::resource('guest','GuestController');
Then in my controller:
public function edit($id){
$guest=Guest::find($id);
return view('Guests.edit1',compact('guest'));
}
The problem with this is that the id appears in my route, which I consider is not safe, it appears in this way
http://localhost:8081/guest/2/edit
How do I solve in such a way that the id is not shown in the url, or the url is not shown at all
A possible solution would be to use Encryption to display your encrypted parameter. From the view, I would use
encrypt
fromblade
Then in the controller decrypt this value.
Do not forget
use Illuminate\Support\Facades\Crypt;
I found the best solution to the problem using
FakeID
.Masking all the ids of your model, simply by putting the namespace and the trait , it will hide your id and it will generate a very clean URL, example, if you have something like this:
I would change it to something like this:
You don't have to worry about encrypting or decrypting , it does everything automatically.
You could also try using friendly urls, you have a cleaner url and you manage to hide the id.
I use this package https://github.com/cviebrock/eloquent-sluggable it allows you to generate friendly urls in a simple way.
Try it like this with an alias on the path:
or also with this laravel helper:
I know it's a bit late now but I just found a possible solution.
php artisan make:policy NombrePolicy -m nombreModelo
. Go to app->HTTP->policies.Now here are two ways that I look at...
$urlUserBase=request()->url();
. Then I get the edit route but from the current user with$urlUserActual=route('usuario.edit',['usuario'=>$user->id]);
returns comparing that if they are identicalreturn $urlUserActual === $urlUserBase;
in your controller you write//Verifica el policy User@update $this->authorize('función',$varConArrayUsuario);
The result is this:
And the other you can check here