Revathi
Revathi

Reputation: 57

How pass Dynamic variable from url in Laravel

I need to fetch the data from database and display it view . This is my web.php

Route::get('/businesscard/{name}',  function ($name) {
    
    
//$username= App\users::where('username', $name);


$username=DB::table('users')->select('*')
                     ->where('username', '=', $name)
                    // ->groupBy('status')
                     ->get();
   
    return view('auth.pro')->with(['username' => $username]);
   
   
   
     return array(
       'name' => $name
       );
       
       
       
});

If the user enters domain.com/businesscard/username I need to fetch the data for the username and display it in view .It is working.But I need to remove businesscard .User need to enter domain.com/username. I have tried the below code.

Route::get('/{name}',  function ($name) {
    
    
//$username= App\users::where('username', $name);


$username=DB::table('users')->select('*')
                     ->where('username', '=', $name)
                    // ->groupBy('status')
                     ->get();
   
    return view('auth.pro')->with(['username' => $username]);
   
   
   
     return array(
       'name' => $name
       );
       
       
       
});

If there is data it is working .but other pages are not working like login and register .Users are entering their username in registration .

Upvotes: 1

Views: 1384

Answers (2)

Danny Van Der Sluijs
Danny Van Der Sluijs

Reputation: 326

The order of your route matters. See order of route declarations in laravel package

So the /{name} should be registered as the last route to avoid matching for other routes.

Upvotes: 1

Senthurkumaran
Senthurkumaran

Reputation: 1858

/{name} it means / with any value. If you try /login or /register.Then your logic is confused with this so that other pages are not working. Best way to develop as you expect like first one.

Another thing in your code there is two return second one is not doing anything. After the first one it return to view so second one unused. remove that return as well.

Upvotes: 0

Related Questions