anaval
anaval

Reputation: 1138

Laravel controller parameter's purpose?

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

Why do we put parameters on laravel controllers when we can use "Input::get(id)"? Can anyone give a sample on a situation where I need to use parameters on controller?

Upvotes: 1

Views: 67

Answers (1)

Dipendra Gurung
Dipendra Gurung

Reputation: 5870

In your condition when you are using Input::get('id'), the request to show the profile will be something like

yourdomain.com/profile?id=2

But if you want to show the profile of a user based on something like this one,

yourdomain.com/profile/2

or

yourdomain.com/profile/john

you need a controller parameter.

In the seconds cases you can simply then, bind your parameter to the User model.

public function showProfile(User $user)
{
    return view('user.profile', ['user' => $user);
}

Upvotes: 1

Related Questions