我在操作中使用 url 并得到 404 not found 但路由存在,当我使用路由时我得到错误 route is not defined in Laravel

I use url in action and get 404 not found but route is exist and when I use route I get error route is not defined in Laravel

我想要在数据库中存储 speaker/book 的订单,但我不使用图表,因此在填写文本字段后,它将被插入到数据库 table 中并且只记录- 在用户可以进行预订。我下面的程序逻辑流程是不是错了?当我使用这个

<form method="POST" action="{{ url('speaker/book-store') }}">
    @csrf
</form>

我收到错误 404 未找到,但我在 web.php 中的路线存在 这里 web.php.

Route::get('/speaker/book', ' App\Http\Controllers\ProductController@create')-> name('speaker/book-create');
Route::post('/speaker/book', 'App\Http\Controllers\ProductController@store')->name('speaker/book-store');

但是当我使用路由时不是这样 URL:

<form method="POST" action="{{ route('speaker/book-store') }}">
    @csrf
</form>

我得到错误路由未定义:

这是我的ProductController

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\User;
use Auth;
class ProductController extends Controller {
    public function create() {
        return view('speaker/book');
    }

    public function store(Request $request) {
        $order =new Order();
        $order->date=$request->input('date');
        $order->date=$request->input('time');
        $order->date=$request->button('acara');
        $order->date=$request->textarea('detail');
        Auth::user()->orders()->save($order);

        return redirect()->back()->with('success'); 
    }
}

型号订单

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends Model {
    use HasFactory;

    public function user() {
        return $this-> belongsTo('App\User');
    }
}

我这样添加的模型用户:

public function orders() {
    return $this->hasMany('App\Order');
}

更新这些路线。

Route::get('/speaker/book',[App\Http\Controllers\ProductController::class, 'create'])-> name('speaker/book-create');
Route::post('/speaker/book', [App\Http\Controllers\ProductController::class, 'store'])->name('speaker/book-store');

然后 运行 php artisan optimize

我希望你能得到你的解决方案。

您定义的路由不正确 URL 不应该相同

旧路线

Route::get('/speaker/book', function() { return view('speaker/book'); }); 

Route::get('/speaker/book', ' App\Http\Controllers\ProductController@create')
 ->name('speaker.book-create');

Route::post('/speaker/book', 'App\Http\Controllers\ProductController@store')
 ->name('speaker.book-store'); 

新路线 - 尝试这条路线

Route::get('/speaker/book/view', function() { return view('speaker/book'); });

Route::get('/speaker/book/create', 
'App\Http\Controllers\ProductController@create')->name('speaker.book-create');

Route::post('/speaker/book/store', 
'App\Http\Controllers\ProductController@store')->name('speaker.book-store'); 

book.blade.php

正在行动
 {{ route('speaker.book-store') }}

了解一件事,即使提供到 form-action 的路径,在发布表单时它会转换为 URL 因为你相同的 URL 定义它变得冲突