Laravel 路由模型绑定:destroy 方法没有获取必要的信息

mar*_*605 3 php laravel laravel-5.7

这是我的 ProducerType 模型控制器:

namespace App\Http\Controllers;

use App\ProducerType;
use App\Http\Requests\ValidateProducerTypes;
use Illuminate\Http\Request;

class ProducerTypeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    ...

    public function destroy(ProducerType $producerType)
    {
        $producerType->delete();
        return redirect('/producers');
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProducerType extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nome'
    ];
}
Run Code Online (Sandbox Code Playgroud)

看法:

<form action="/producers-type/{{ $type->id }}" method="POST">
    @csrf
    @method('DELETE')
    <button type="submit" class="btn-icon">
        <img src="{{ asset('images/times-circle-regular.svg') }}" alt="">
    </button>
</form>
Run Code Online (Sandbox Code Playgroud)

路线:

Route::resource('producers-type', 'ProducerTypeController', [
    'only' => ['store', 'update', 'destroy']
])->middleware('permission:create users');
Run Code Online (Sandbox Code Playgroud)

我的问题是:$producerType变量没有获取必要的属性。

mar*_*605 5

虽然上面的答案给出了问题的解决方案,但我最终在我的代码中找到了问题!

来自Route Model Binding的 Laravel 文档:

由于 $user 变量被类型提示为 App\User Eloquent 模型,并且变量名称与 {user} URI 段匹配,Laravel 将自动注入具有与请求 URI 中相应值匹配的 ID 的模型实例。如果在数据库中找不到匹配的模型实例,则会自动生成 404 HTTP 响应。

这是我的问题:我正在使用$producerType变量,而 Laravel 正在等待,$producers_type因为我的路线producers-type/{producers_type}.