0

I am new to Laravel and web programming things. I saw lecturer in tutorial, he passes an id to a controller by using controller parameter
Route::get('/post/{id}', ['as'=>'home.post', 'uses'=>'AdminPostsController@post']); , what is the difference comparing with passing an id through $request parameter from controller?
could you tell me when to use either controller parameter and request.

2 Answers2

1

One way to explain it is to refer to the HTTP verb GET you are refering to.

For a GET request to return a post where the id is 1 you will have two options:

  1. /post/{id}

Using this method (a restful convention) you will need to pass the variable as a parameter to your controller action to access it.

public function view($id)
{
    $post = Post::find($id);
    ...
}
  1. /post?id=1

Using this method you pass the id as a query string parameter inside the url. Then inside a controller you access the value from the request.

public function view(Request $request)
{
    $post = Post::find($request->input('id'));
    ...
}

Now lets say you want to create a new Post that would typically be an HTTP verb POST request to a /post endpoint where you access the payload of the form using the Request.

public function create(Request $request)
{       
    $post = Post::create($request->only('description'));
}

Now lets say you want to update a current Post that would typically be an HTTP verb PUT request to a /post/{id} endpoint where you will fetch the model via the parameter and then update the data using the request.

public funciton update(Request $request, $id)
{
    $post = Post::find($id);
    $post->update($request->only('description'));
}

So at times you will use a combination of controller parameters with the request as well. But generally the controller parameter is for single items inside the routes that you need to access.

Leon Vismer
  • 4,925
  • 1
  • 20
  • 22
  • In your `update` function example, the `id` is passed by using `/post/{id}`. I tried to use similar example `public function myUpdate(Request $request)` and use `/post` endpoint. I also could get the same `id` by using `public function myUpdate(Request $request){ $post = Post::find($request['id']); $post->update($request->only('description'));}`. Do both ways are allowed? – Fajar Sidiq Salviro Oct 30 '17 at 07:16
  • No chances are the id on your side was part of the form, perhaps a hidden field? When updating a resource it is better to be explicit with your endpoint ie. ```/post/{id}``` not just ```/post``` – Leon Vismer Oct 30 '17 at 09:16
  • yes, I mean a hidden field. Is it acceptable to update and pass the id using hidden field? – Fajar Sidiq Salviro Oct 30 '17 at 09:18
  • Sure, it is acceptable but personally it is better to be more explicit. I'd say it is good to stay as close to resource/restful [endpoints](https://laravel.com/docs/5.5/controllers#resource-controllers). As an example what would your ```create``` route be then? – Leon Vismer Oct 30 '17 at 09:29
  • Do youe mean this? `admin/posts/create`, this route named `admin.posts.create` I define this using Route::resource, and `/post/{id}` purpose is just showing post. Sorry I still have poor understanding in Laravel terms, but thank you for the information tho – Fajar Sidiq Salviro Oct 30 '17 at 09:44
  • Yes, however if you use ```Route::resource``` then ```/post/{post}``` could point to three different controller methods depending on the HTTP verb you use, one for showing a post (GET), one for updating a post (PUT) and one for deleting a post (DELETE). Then if you want you can use the named route inside your views using the ```route()``` function or the ```url()``` would also be fine. – Leon Vismer Oct 30 '17 at 09:50
  • yep, thank you. This made me assured with what i am learning. – Fajar Sidiq Salviro Oct 30 '17 at 09:55
0

Assuming you are the newbie in Web development, especially in Laravel, I suggest you read Laravel documentation. posts/{id} retrieve the post model value that corresponds to that ID.

Route::get('/post/1', 'AdminPostsController@post']); -> returns post that has an ID 1.

When send you request like this posts/1 it will inject your model and returns corresponding id value

Or you can handle manually through controller with corresponding id.

public function posts(Request $request)
{
    returns Posts::find($request->id);
}
Orgil
  • 691
  • 7
  • 16
  • Ok, I am sorry for being unclear. I got the idea about how to pass id variable through controller parameter, then what is the difference if we just pass it through $request parameter. For example: `public function post(Request $request){$data['id'] = $request['id']}` with route: `Route::get('/post/{id}', 'AdminPostsController@post']);` – Fajar Sidiq Salviro Oct 30 '17 at 06:41
  • When exactly I should handle manually through controller `request` or inject using post/{id}? – Fajar Sidiq Salviro Oct 30 '17 at 07:25