ajun 5 лет назад
Родитель
Сommit
92e465d685

+ 11 - 2
app/Http/Controllers/MaterialController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers;
 
+use App\Exceptions\WarningException;
 use App\Filters\MaterialFilters;
 use App\Material;
 use Illuminate\Http\Request;
@@ -25,8 +26,16 @@ class MaterialController extends Controller
 
     }
 
-    public function destroyApi(Request $request)
+    public function destroyApi($id):array
     {
-
+        /** @var Material $material */
+        $material = Material::query()->find($id);
+        try {
+            $bool = $material->delete();
+            if($bool>0)return ['success'=>true];
+            else return ['success'=>false,'message'=>'删除失败'];
+        } catch (WarningException $e) {
+            return ['success'=>false,'message'=>$e->getMessage()];
+        }
     }
 }

+ 3 - 3
resources/views/maintenance/material/_table.blade.php

@@ -5,13 +5,13 @@
         <th>名称</th>
         <th>操作</th>
     </tr>
-    <tr class="text-center" v-for="material in materials">
+    <tr class="text-center" v-for="(material,i) in materials">
         <td>@{{ material.id }}</td>
         <td>@{{ material.code }}</td>
         <td>@{{ material.name }}</td>
         <td>
-            <button class="btn btn-sm btn-outline-primary">编辑</button>
-            <button class="btn btn-sm btn-outline-danger" @click="destroy(material.id)">删除</button>
+            <button class="btn btn-sm btn-outline-primary" @click="edit(material)">编辑</button>
+            <button class="btn btn-sm btn-outline-danger" @click="destroy(material.id,i)">删除</button>
         </td>
     </tr>
 </table>

+ 8 - 6
resources/views/maintenance/material/index.blade.php

@@ -1,5 +1,5 @@
 @extends('layouts.app')
-@section('title')承运商@endsection
+@section('title')耗材类型@endsection
 
 @section('content')
     <span id="nav2">
@@ -25,20 +25,22 @@
             materials:{!! $materials->toJson() !!}['data']
         },
         methods:{
-            destroy($id){
-                axios.delete('api/material/destroy',{'id':$id}).then(res=>{
+            destroy(id,index){
+                axios.delete('{{url("apiLocal/material/destroy")}}/'+id).then(res=>{
                     if(res.data.success){
                         tempTip.setDuration(3000);
                         tempTip.showSuccess('删除成功');
+                        this.$delete(this.materials,index);
                         return;
                     }
                     tempTip.setDuration(3000);
                     tempTip.show(res.data.message);
-                }).error(err=>{
+                }).catch(err=>{
                     tempTip.setDuration(3000);
                     tempTip.show(err);
-                })
-            }
+                });
+            },
+
         }
     });
 </script>

+ 1 - 1
routes/apiLocal.php

@@ -113,5 +113,5 @@ Route::group(['prefix'=>'control'],function () {
 Route::group(['prefix' => 'material'],function(){
     Route::get('store','MaterialController@storeApi');
     Route::get('update','MaterialController@updateApi');
-    Route::get('destroy','MaterialController@destroyApi');
+    Route::delete('destroy/{id}','MaterialController@destroyApi');
 });