Laravel 共享主机 ajax 删除

Laravel Shared hosting ajax delete

出于测试目的,我在免费托管上托管了我的网站,一切正常,但 Ajax 删除了。当我点击删除时,删除功能运行并且所有内容都被删除,但由于某种原因它 returns 500 错误。在本地它可以正常工作。

Route::delete('/admin/deleteRound', 'AdminCyclesController@deleteRound')->name('admin.deleteRound');  

$.ajax({
    type: "POST",
    url: urlDeleteRound,
    data: {cycle_id: cycle_id, round: round, _token: token, _method: 'delete'}
}).success(function (response) {.....});

我尝试了所有可以在网上找到的方法,但都没有成功。有没有办法解决这个问题,或者至少有办法找出问题所在?

已编辑 - .log

我不知道该怎么做。

local.ERROR: SQLSTATE[HY000]: General error (SQL: DELETE FROM cycle_team where cycle_team.cycle_id=9 and cycle_team.round=1) {"userId":1,"email":"xxxxxx@gmail.com","exception":"[object] (Illuminate\Database\QueryException(code: HY000): SQLSTATE[HY000]: General error (SQL: DELETE FROM cycle_team where cycle_team.cycle_id=9 and cycle_team.round=1) at /storage/ssd5/708/6079708/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: HY000): SQLSTATE[HY000]: General error at /storage/ssd5/708/6079708/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:332)

编辑 2 - 执行删除的代码

public function deleteRound(Request $request){

    $round=$request['round'];
    $id=$request['cycle_id'];

    DB::select("DELETE FROM `cycle_team` where cycle_team.cycle_id=$id and cycle_team.round=$round");

    $teams = DB::select("SELECT teams.id, teams.title,sum(ct.points) + sum(ct.is_winner) + sum(ct.is_over) as points, sum(ct.points) + sum(ct.is_winner) + sum(ct.is_over)-Min(ct.points + ct.is_winner + ct.is_over) as minpoints, COUNT(teams.id)-1 as number FROM `teams`INNER JOIN cycle_team as ct on teams.id =ct.team_id INNER JOIN cycles as c on c.id = ct.cycle_id where ct.cycle_id =$id > 0 GROUP BY ct.cycle_id, ct.team_id, teams.title, teams.id order by points desc");

    return response()->json(['teams'=>$teams]);
}

解决方案

 DB::select("DELETE FROM `cycle_team` where cycle_team.cycle_id=$id and cycle_team.round=$round")

一直在制造问题,使用 Builder 解决了问题

DB::table('cycle_team')->where('cycle_id', id)->where('round', $round)->delete();

您正在使用 DB::select(),它在后台默认使用 只读 PDO 实例。由于 DELETE 是写入操作,因此出现一般错误。

考虑使用 DB::delete() 方法而不是 DB::select(),因为这是您正在执行的操作类型。

您也可以使用 DB::statement(),其中 returns 是一个基于查询是否成功的布尔值,或者 DB::affectingStatement() 如果您想要查询影响的行数.

或者,按照评论中的建议,使用查询构建器构建删除查询。

DB::table('cycle_team')
    ->where('cycle_id', $id)
    ->where('round', $round)
    ->delete();