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

bevA's avatar
Level 1

Get data without an ID in the url.

I want to fetch topics from my data base but want to show name of the topic in lower case with underscore in place of any character and space. Right now I am getting data via Id.

Route::get('/topics/{id}', 'TopicsController@showById')->where('id', '[0-9]+');

With the result

{"id":2,"topic":"Motivation M.Block","status":1}

and url is

http://example.com/topics/2

I would like to have it as

http://example.com/topics/motivation_m_block

and fetch the array data from second table with topic id i.e. 2 in this case.

0 likes
8 replies
bevA's avatar
Level 1

Thanks @Vilfago, it worked the problem is now, I have created the slug column in the table but how can I get the content for that page when we click on the url.

foreach ($topics_list as $topics)
{
echo "<li><a href=\"topics/".$topics->slug."\">".$topics->name."</a></li>";
}   

How can I send info like it's id through url to fetch it on on new page?

munazzil's avatar
foreach ($topics_list as $topics)
{
echo "<li><a href="{{route('/topics/'.$topics->slug.'/')}}">".$topics->name."</a></li>";
}  
bevA's avatar
Level 1

Thanks, @munazzil, after I used your code it says

(2/2) ErrorException Route [/topics/art/] not defined.

Hi, @Snapey, I am new to Laravel and MCV. Can you elaborate Explicit binding a little bit.

bevA's avatar
Level 1

Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Topics extends Model
{
    //Table name
    protected $topics = 'topics';

    //Primery Key
    public $primeryKey = 'id';

    //Time Stamps
    public $timestamps = true;
}

Controller

public function index()
    {
        $topics = DB::table('topics')->limit(30)->get();
        return view('pages.topics',  ['topics' => $topics]);
    }

    public function showById()
    {
        $topics = Topics::find($id);
        return view('pages.topics', ['topics' => $topics]);
    }
munazzil's avatar

@bevA

Use like below controller You have to pass $id parameter in your controller function.

public function index()
    {
      $topics = DB::table('topics')->limit(30)->get();
        return view('pages.topics',  ['topics' => $topics]);
    }

 public function showById($id)
 {
       $topics = Topics::findorFail($id);
         return view('pages.topics', ['topics' => $topics]);
  }

Please or to participate in this conversation.