Home > Blockchain >  how to get name from url dynamically
how to get name from url dynamically

Time:05-14

How to get a part of the url?

query to be made:

$name = 'LeSant'; // (from url dinamic) //
$event = Dias::where('name', $name)->first();

enter image description here

How can I get name from the url? /event/LeSant

CodePudding user response:

Multiple ways to do that. You can use request helper for it. Below two ways with requesthelper:

  1. request()->segment(count(request()->segments()))
  2. last(request()->segments())

CodePudding user response:

Can you please run this:

var url = 'https://myurl.com/event/LeSant';

var url_to_array = url.split('/');

console.log(url_to_array[url_to_array.length-1]);

CodePudding user response:

It depends on how you are declared this route. You can specify model parameter and with laravel route binding you can inject model in your controller method https://laravel.com/docs/9.x/routing#customizing-the-key

In routes/web.php:

Route::get('/events/{event:name}', [EventController::class, 'show']);

And in EventController controller you can use something like that:

public function show(Event $event)
{
    // do something with $event
}

Or if it not suits to you can get last element from request()->segments()

  • Related