douba9776 2015-11-09 09:36
浏览 96
已采纳

尝试从数据库中删除某些内容时,RouteCollection.php中的Laravel 5.1 NotFoundHttpException

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
  • 写回答

1条回答 默认 最新

  • dongpu3347 2015-11-09 09:49
    关注

    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']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#ar#的问题:/* User can add his own implementation to report the HAL error return state */(语言-c语言)
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败