PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 17, 2022

[FIXED] how to get name from url dynamically

 May 17, 2022     laravel, laravel-5, php     No comments   

Issue

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


Solution

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()



Answered By - Mykola Bokoch
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing