这是在控制器MessagesController
中:
public function destroy(Messages $id)
{
\DB::delete(
'DELETE FROM messages WHERE id=' . $id
);
return redirect()->route('messages.index')->with('message', 'Deleted.');
}
这是routes.php:
Route::delete('messages.destroy', ['as' => 'messages.destroy', 'uses' => 'MessagesController@destroy']);
这是视图文件:
{!! Form::open(array('route'=>['messages.destroy',$message->id],'method'=>'DELETE')) !!}
{!! Form::button('Delete',['class'=>'btn btn-danger','type'=>'submit']) !!}
{!! Form::close() !!}
所以,我有一个错误:
SQLSTATE [42000]:语法错误或访问冲突:1064您有 SQL语法错误;查看与您的手册相对应的手册 MariaDB服务器版本,用于在' []'附近使用正确的语法在第1行 (SQL:DELETE FROM messages WHERE id = [])
据我所知,控制器没有id。怎么解决这个问题?
P.S。另外,在控制器中尝试这样:
public function destroy(Messages $message)
{
$message->delete();
return redirect()->route('messages.index')->with('message', 'Deleted.');
}
它只显示消息Deleted
。但它没有删除任何东西。
答案 0 :(得分:1)
以下是我正在做的事情的一个例子:
routes / web.php:
Route::resource('products', 'Products');
控制器:
public function destroy($id)
{
Product::destroy($id);
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
查看:
{!! Form::open(['method' => 'DELETE','route' => ['products.destroy', $product->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
答案 1 :(得分:0)
尝试这条路线:
Route::delete('messages.destroy/{id}', ['as' => 'messages.destroy', 'uses' => 'MessagesController@destroy']);
和这个控制器:
public function destroy($id)
{
Message::destroy($id);
return redirect()->route('messages.index')->with('message', 'Deleted.');
}