Reputation: 79
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
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
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