Moussa Chaabar
Moussa Chaabar

Reputation: 501

Laravel 5.1 NotFoundHttpException in RouteCollection.php when trying to delete something from database

I am building an application where users can like/unlike each others projects. I have build a system that allow users to like the work and every like gets stored in my database with a unique id, the project_id and the user_id.

Now I am building the Unlike part and get an error when hitting the Unlike button.

The error:

Sorry, the page you are looking for could not be found. NotFoundHttpException in RouteCollection.php line 161:

My routes:

Route::post('projects/{id}', 'LikesController@store');
Route::get('projects/{id}','LikesController@destroy');

My Controller

public function store(Request $request)
{
    $input = Request::all();
    $like = new Like;
    $like->user_id = Auth::user()->id;
    $like->project_id = $input['project_id'];
    $like->save();
    return redirect('projects/'.$input['project_id']);
}

public function destroy($id)
{
    $input = Request::all();
    Like::find($id)->delete();
    return redirect('projects/'.$input['project_id']);
}

My form

@if (Auth::check())
                @if ($isLikedUser)
                    {!! Form::open(array('url'=>'projects/'.$project->id.'/deletelike','method'=>'POST')) !!}
                    {!! Form::hidden('project_id', $project->id) !!}
                    {!! Form::Submit('Unlike', array('class'=>'send-btn')) !!}
                    {!! Form::close() !!}
                @else
                    {!! Form::open(array('url'=>'projects/'.$project->id,'method'=>'POST', 'id'=>'likeform')) !!}
                    {!! Form::hidden('project_id', $project->id) !!}
                    {!! Form::Submit('Like', array('class'=>'send-btn')) !!}
                    {!! Form::close() !!}
                @endif
    @else
            <p>Log in to like.</p>
    @endif

Upvotes: 0

Views: 720

Answers (1)

Milan Maharjan
Milan Maharjan

Reputation: 4236

Your Route states that delete is done when the method id GET. But in your unlike button your form method is POST. Change that to GET. And also the action url you are giving for the delete is 'projects/'.$project->id.'/deletelike' But in your routes you dont have a route structured like this. Also if you post this using GET, then hidden project_id is not required since it is already passed in the url. So your delete form should look like this

{!! Form::open(array('url'=>'projects/'.$project->id,'method'=>'GET')) !!}
{!! Form::Submit('Unlike', array('class'=>'send-btn')) !!}
{!! Form::close() !!}

Edit 1 Better change your Routes like this, and use post for both like and unlike:

Route::post('projects/like/{id}', 'LikesController@store');
Route::post('projects/unlike/{id}','LikesController@destroy');

and in your form

{!! Form::open(array('url'=>'projects/like'.$project->id,'method'=>'POST')) !!}

and for unlike form

{!! Form::open(array('url'=>'projects/unlike/'.$project->id,'method'=>'POST')) !!}

and your delete method should be

public function destroy($id)
{
    $input = Request::all();
    Like::whereProjectId($id)->whereUserId(Auth::user()->id)->delete();
    return redirect('projects/'.$input['project_id']);
}

Upvotes: 1

Related Questions