我想在laravel 5.5中进行多次删除,我尝试过的表单没有用,所以我决定使用ajax。
controller
public function multipledel(Request $request){
$deli = $request->input('productsfordel'); //get id's of selected post/products
$product = Product::where('id', [$deli]); //find those id's in DB
$product->delete(); // Delete them
Session::flash('success', 'Selected products are successfully deleted.');
return redirect()->route('products.index');
}
route
Route::post('delmultipleproducts', 'ProductController@multipledel')->name('delmultipleproducts');
ajax
<script type="text/javascript">
$(document).ready(function() {
$('#multidel').on('click', function(e) { //define which button to click for multiple delete
e.preventDefault();
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
var idss = $('#productsfordel').val(); //where to get id's from
if(idss) {
$.ajax({
url: '{{ url('admin/delmultipleproducts') }}',
type: "POST",
dataType: "json",
success:function(data) {
console.log('working');
}
});
}else{
console.log('not working');
}
});
});
</script>
blade
<button id="multidel" name="multidel">Delete All</button>
<thead>
<th class="text-center">xxxxx</th>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>
<input type="checkbox" id="productsfordel" name="productsfordel[]" value="{{ $product->id }}" />
</td>
// rest of table
这就是我在网络中得到的结果:
有什么想法吗?
基于 Babak 回答我可以在网络上获得一些结果,这意味着id实际上正在发送,这里是错误:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`mysite`.`product_relatives`, CONSTRAINT `product_relatives_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)) (SQL: delete from `products` where `id` = 48)
因此我意识到必须对我的产品同步方法(例如选项和规范)执行某些操作,并将我的功能更改为:
public function multipledel(Request $request){
$deli = $request->input('productsfordel');
$product = Product::where('id', [$deli]);
// added from here
$product->suboptions()->detach();
$product->subspecifications()->detach();
if(!empty($product->imageOne)){
Storage::delete($product->imageOne);
}
if(!empty($product->imageTwo)){
Storage::delete($product->imageTwo);
}
//to here
$product->delete();
Session::flash('success', 'Selected products are successfully deleted.');
return redirect()->route('products.index');
}
现在我得到了:
Call to undefined method Illuminate\Database\Query\Builder::suboptions()
任何想法?
答案 0 :(得分:1)
你给你的输入相同的id:不是一个好主意。首先,您应该从输入标记中删除id属性。
然后,您最好将输入封装在带有ID的表单标记中(让我们说#34; myForm&#34;):
<form id="myForm">
<table>
// ...
@foreach($products as $product)
<tr>
<td>
<input type="checkbox" id="productsfordel" name="productsfordel[]" value="{{ $product->id }}" />
</td>
// ...
</table>
</form>
由于路由是在Ajax请求中定义的,因此无需设置任何操作方法。
谈论你的Ajax请求,不要忘记用这样的数据属性传递你的数据(即你的数组):
...
dataType: "json"
data : $('#myForm').serialize()
因此控制器会收到一个id数组,您可以使用Eloquent的destroy方法删除它们:
Product::destroy($request->input('productsfordel'));
编辑:如果您不想使用表单,您可以改为为您的输入提供一个类而不是一个id(class =&#34; someClass&#34;)和然后用
传递你的数据data: $('.someClass:checked').serialize(),
答案 1 :(得分:1)
首先将数据添加到您的ajax
var idss = $('#productsfordel').val();
if(idss) {
$.ajax({
url: '{{ url('admin/delmultipleproducts') }}',
type: "POST",
dataType: "json",
data: {
productsfordel: idss
}
success:function(data) {
console.log('working');
}
});
}else{
console.log('not working');
}
然后在控制器中添加一个foreach,如下所示
$deli = $request->get('productsfordel'); //get id's of selected post/products
Foreach($deli as $item){
$product = Product::find($item); //find those id's in DB
$product->delete(); // Delete them
}