satik-stack
satik-stack

Reputation: 79

Laravel5: Can't delete from DB

This is in controller MessagesController:

public function destroy(Messages $id)
{
    \DB::delete(
            'DELETE FROM messages WHERE id=' . $id
        );
    return redirect()->route('messages.index')->with('message', 'Deleted.');
}

This is routes.php:

Route::delete('messages.destroy', ['as' => 'messages.destroy', 'uses' => 'MessagesController@destroy']);

This is view file:

{!! Form::open(array('route'=>['messages.destroy',$message->id],'method'=>'DELETE')) !!}
{!! Form::button('Delete',['class'=>'btn btn-danger','type'=>'submit']) !!}
{!! Form::close() !!}

So, I have an error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[]' at line 1 (SQL: DELETE FROM messages WHERE id=[])

I understand that there is no id going to controller. How to solve this?

P.S. Also, tried like this in controller:

public function destroy(Messages $message)
{
    $message->delete();
    return redirect()->route('messages.index')->with('message', 'Deleted.');
}

It's just showing message Deleted. but it's not deleting a thing.

Upvotes: 1

Views: 76

Answers (2)

Anton Grigorov
Anton Grigorov

Reputation: 139

Try this route:

Route::delete('messages.destroy/{id}', ['as' => 'messages.destroy', 'uses' => 'MessagesController@destroy']);

and this controller:

public function destroy($id)
{
    Message::destroy($id);
    return redirect()->route('messages.index')->with('message', 'Deleted.');
}

Upvotes: 0

Harold Scholtz
Harold Scholtz

Reputation: 99

Here is an example of what i'm doing:

routes/web.php :

Route::resource('products', 'Products');

controller:

public function destroy($id)
{
        Product::destroy($id);
        return redirect()->route('products.index')
                        ->with('success','Product deleted successfully');
}

View:

{!! Form::open(['method' => 'DELETE','route' => ['products.destroy', $product->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

Upvotes: 1

Related Questions