【发布时间】:2018-05-04 16:57:57
【问题描述】:
我尝试将多项选择添加到我的应用并一次删除数据,但它们不会从数据库中删除。
代码
controller
public function multipledel(Request $request){
$deli = $request->input('productsfordel');
$product = Product::whereIn('id', $deli);
$product->delete();
return redirect()->route('products.index')->with('success', 'Selected products are successfully deleted.');
}
route
Route::post('delmultipleproducts', 'ProductController@multipledel')->name('delmultipleproducts');
blade
//multiple delete form starts before table begin
{!! Form::open(['method' => 'Post', 'route' => ['delmultipleproducts'] ]) !!}
//delete button
<button name="bulk_delete" id="bulk_delete" data-toggle="tooltip" data-placement="top" title="Delete Selected" class="btn btn-xs btn-theme04" type="submit"><i class="fa fa-trash-o"></i></button>
<thead>
//table headears
</thead>
<tbody>
@foreach($products as $product)
<tr>
//my checkbox's
<td>
<input type="checkbox" name="productsfordel[]" class="checkboxes" value="{{ $product->id }}" />
</td>
// my multiple delete form closes here
{!! Form::close() !!}
//rest of the table...
问题
- 我遇到了几种错误!
第一
为 foreach() 提供的参数无效
秒
SQLSTATE[23000]:违反完整性约束:1451 无法删除或 更新父行:外键约束失败
第三
它返回我的成功会话并说 Selected products are successfully deleted. 但他们不是。
意见
我认为我得到的错误是指我的产品附加数据,在我的普通删除方法 one by one 中,我有类似下面的代码
public function destroy($id)
{
$product = Product::findOrFail($id);
$product->suboptions()->detach();
$product->subspecifications()->detach();
if(!empty($product->imageOne)){
Storage::delete($product->imageOne);
}
if(!empty($product->imageTwo)){
Storage::delete($product->imageTwo);
}
$product->delete();
return redirect()->route('products.index')->with('success','Product successfully deleted');
}
如您所见,我有几个条件并将数据同步到我的每一个产品,但在我的多重删除方法中我没有它们,但是我也尝试将它们包括在内,但它在$product = Product::whereIn('id', $deli); 上给了我错误不确定我是否需要以某种方式排列我的$deli?
有什么想法吗?
更新
我意识到没有任何东西会发送到我的控制器,当我尝试dd($deli); 时,我得到了 null 而不是 id 的选定产品。
【问题讨论】:
-
我认为问题出在您的刀片文件上,您可以粘贴刀片文件的整个代码吗?