StanleyHuxley
StanleyHuxley

Reputation: 3

Laravel Shared hosting ajax delete

I hosted my site on freehosting for testing purposes, and everything works perfectly but Ajax delete. When I click on delete, delete function goes through and everything is deleted, but for some reason it returns 500 error. Locally it works without a problem.

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

and

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

I tried everything I could find online but without success. Is there a way to fix this, or at least a way to figure out where the problem lies?

EDITED - .log

I don't know what to make out of this.

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":"[email protected]","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)

Edit 2 - Code perfoming deletion

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]);
}

SOLUTION

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

was making problems, using Builder solves the problem

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

Upvotes: 0

Views: 89

Answers (1)

Aken Roberts
Aken Roberts

Reputation: 13447

You're using DB::select() which, under the hood, uses a read-only PDO instance by default. As DELETE is a write operation, the general error is occurring.

Consider using the DB::delete() method instead of DB::select(), since that's the type of operation you're performing.

You could also use DB::statement(), which returns a boolean based on the success of the query, or DB::affectingStatement() if you'd like the number of rows affected by the query.

Or, as suggested in the comments, use the Query Builder to build a delete query.

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

Upvotes: 1

Related Questions