Missing required parameter on form action

Viewed 319

First of all, I would like to say that my English is not so good and is my first time posting here, so excuse me if I did something wrong! Well, I am starting practicing with Laravel and I am trying to create a URL for users can like posts. my URL and controller is right now this Route::post('/post/{post}/like', [LikeController::class, 'postLike'])->name('post.like'); where post is the posts id that i am trying to pass through my form action attribute. Here is my form:

@props(['id'])

<div {{ $attributes->merge(['class' => 'card-footer d-flex justify-content-around']) }}>
    <form action= "{{ route('post.like' , $id) }}"  method="post" >
        @csrf
        <button>submit</button>
    </form>
</div>

If you wonder if the id is not actually passed into the component, I checked {{ dd($id) }} and it printed it.

My controller is this which it doesn't actually do anything right now. I am just trying to pass the id:


class LikeController extends Controller
{
    public function postLike(Post $post) {
        dd($post);
    }
}

After all this i am getting the error:

Missing required parameter for [Route: post.like] [URI: post/{id}/like] [Missing parameter: id]. (View: blog\resources\views\components\post\interaction.blade.php)

I am having two days to find the problem and I am still trying this moment I am sending this... I can't found where is the mistake! If you could help me would be much appreciated! Ty in advance

4 Answers

You need to pass the ID as an associative array in your route().

{{ route('post.like', ['id' => $id]) }}

If the named route defines parameters, you may pass the parameters as the second argument to the route function. The given parameters will automatically be inserted into the generated URL in their correct positions and you should name 'post' instead of 'id':

Route::post('/post/{post}/like', [LikeController::class, 'postLike'])->name('post.like');

And you can call this route anywhere

route('post.like', [$postId])

If that still doesn't work for you. Then it might be issue of Route Cache Run:

php artisan route:clear

Use same name parameter

When you use "id" keyword as a parameter in web.php then also same name pass in function argument in the controller

Route::post('/post/{id}/like', [LikeController::class, 'postLike'])->name('post.like');

Controller

class LikeController extends Controller
    {
        public function postLike(Post $id) {
        dd($id);
        }
    }

Sorry for my bad English.

I have no idea this could be the problem but after many changes i cast the id of the post into an integer and it actually worked <form action= "{{ route('post.like' , (int)$id) }}" method="post" > . I don't know why i should cast the id into an integer because its already an integer by default and i checked that with {{ dd(getType($id)) }} and printed "integer"! I am really confused but it works! But i am not happy because i have no idea why i should cast an integer into an integer to make it work! Doesn't make any sense! If anyone has an answer to this would be great!

Related