我try 对简单的CRUD使用路由模型绑定,但更新和删除功能不起作用.我用的是LARAVEL 5.5

Route::resource('admin/file','AdminController');

My View For Edit and Delete Buttons

<a href="{{ route('file.edit', ['id'=>$file->id]) }}">

<form action="{{ route('file.destroy', ['id'=>$file->id]) }}" method="post">
   {{method_field('DELETE')}}
   {{csrf_field()}}
   <button type="submit" class="delete">delete</button>
</form>

My Resource Controller :

namespace App\Http\Controllers;

use App\Files;
use Illuminate\Http\Request;

Store Work Fine

  public function store(Request $request)
{
    $this->validate($request,[
        'title'=>'required',
        'body'=>'required',
        'price'=>'required',
        'linkFile'=>'required',
    ]);

     Files::create($request->all());
    return redirect(route('file.index'));
}

但是编辑和删除不起作用

public function edit(Files $files)
{
   return view('admin.edit',compact('files'))->with('title','Edit File');
}

public function destroy(Files $files)
{
    $files->delete();
    return redirect(route('file.index'));
}

我的模特:

protected $table='files';

protected $fillable=[
    'title','body','price','linkFile'
];

当我删除按钮时,没有任何react ,编辑方式相同

If I Add dd($files) at First Column for Edit and Delete Function Then Response Will be [] and There's No Error for handle

这里是我的路由列表

enter image description here

Anyone Can help Please?

推荐答案

最后,两天后我找到了我的答案,我想在这里为所有可能有同样问题的人提供我的答案.

要使路由绑定工作,您的类型提示变量名必须与路由占位符名匹配

例如,我的编辑方法

Here my route URI for edit

admin/file/{file}/edit

如您所见,路由定义中有{file}个占位符,因此相应的变量必须称为$file.

public function edit(Files $file)
{
   return view('admin.edit',compact('file'));
}

Laravel相关问答推荐

当未通过验证的字段是值数组时,Laravel$validator->;validator()方法不会引发异常

提交表格后如何在Livewire 3中调度模式窗口?

如何获取多态多对多关系子查询的自动 id 列名?

Laravel init 查询多种用途

Eloquent的条件过滤器

集成测试模拟外观与注入模拟

Laravel 错误ReflectionException-类 App\Http\Kernel 不存在

Laravel + SQLite = SQLSTATE[HY000]一般错误:8次try 写入只读数据库

Laravel中具有两个外键字段的数据库一对多

在 Laravel 5 中添加新配置文件不起作用

Laravel whereDoesntHave() - 多个 OR 条件

Laravel 将参数从路由传递到过滤器

调用未定义的函数 mb_strimwidth

在 laravel 分页中添加一些数据

Laravel 动作未定义

在 laravel 6.0 中未定义命令ui

Laravel 5.4 有时验证规则不起作用

Laravel 模型:模型属性在哪里?

将自定义消息(或任何其他数据)传递给 Laravel 404.blade.php

如何在 Laravel 中使用补丁请求?