此路由不支持 POST 方法。支持的方法:GET、HEAD in Laravel Inertia

The POST method is not supported for this route. Supported methods: GET, HEAD in Laravel Inertia

我正在为 Laravel Inertia 创建一个简单的 CRUD 应用程序。我正在使用 Laravel v8.24.0 (PHP v7.3.26)。我尝试在互联网上搜索,但其中一些人遇到了 updating/edit 数据。

Lead.vue

<script>
import Layout from "../../Shared/Layout";
export default {
components: {
   Layout,
},
data() {
return {
  lead: {
    name: "",
    email: "",
    phone: "",
    dob: "",
    package: "",
  },
};
},
methods: {
async handleSubmit() {
  let res = await this.$inertia.post("/leads/save", this.lead);
},
},
};

web.php

Route::get('/', [WelcomeController::class, 'index']);
  Route::group(['middleware' => 'auth'], function () {
  Route::get('/dashboard', [DashboardController::class, 'index'])->name('dash');
  Route::get('/leads/add', [LeadController::class, 'create']);
  Route::get('/leads/save', [LeadController::class, 'store']);
});

首席控制器

public function store(Request $request)
{
    $postData = $this->validate($request, [
        'name' => 'required',
        'email' => "required|email",
        'phone' => "required|phone",
        'dob' => "required|date"
    ]);
    $package = "";
    if ($request->has('package')) {
        $package = $request->input('package');
    }
    Lead::create([
        'name' => $postData['name'],
        'email' => $postData['email'],
        'phone' => $postData['phone'],
        'dob' => $postData['dob'],
        'branch_id' => 1,
        'age' => 1,
        'added_by' => Auth::user()->id,
        'interested_package' => $package
    ]);

    return redirect()->route('dash');
}

但是保存时出现错误

Route::post('/leads/save', [LeadController::class, 'store']);

此处 getpost 修复此问题