0

i want to pass arguments from a blade view to a function in the controller

index.blade.php

<a href="{{ route('like', [$post->id, 1])  ) }}" class="like">
    Like
</a>

<a href="{{ route('like', [$post->id, -1])  ) }}" class="like">
    Dislike
</a>

PostController.php

  public function getLikePost($post_id, $like_value)
    {
       $post = Post::find($post_id);
       ...
    }

routes.php

Route::get('like', [
        'uses' => 'PostController@getLikePost',
        'as'   => 'like'
      ]);

but i get an error message

ErrorException in PostController.php line 149:
Missing argument 2 for App\Http\Controllers\PostController::getLikePost()

could anyone help me with this issue?

3 Answers 3

1

Your route should be as:

Route::get('like/{psot_id}/{like_value}', [
        'uses' => 'PostController@getLikePost',
        'as'   => 'like'

And in your view:

<a href="{{ route('like', ['post_id' => $post->id, 'like_value' => 1]) }}" class="like">
    Like
</a>

<a href="{{ route('like', ['post_id' => $post->id, 'like_value' => -1]) }}" class="like">
    Dislike
</a>
Sign up to request clarification or add additional context in comments.

Comments

0

Initialize $post_id and $like_value by empty value as like below.

PostController.php

public function getLikePost($post_id = '', $like_value='')
    {
       $post = Post::find($post_id);
       ...
    }

Comments

0

Try this:

Route.php

Route::get('like/{post_id}/{like_value}', [
        'uses' => 'PostController@getLikePost',
        'as'   => 'like'
      ]);

index.blade.php

<a href="{{ route('like', ['post_id' => $post->id, 'like_value' => 1]) }}" class="like">
    Like
</a>

<a href="{{ route('like', ['post_id' => $post->id, 'like_value' => -1]) }}" class="like">
    Dislike
</a>

Docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.