路径模型绑定不起作用

时间:2017-09-19 18:04:28

标签: php html mysql laravel crud

我正在尝试使用Route Model Binding进行简单CRUD但更新和删除功能不起作用。我正在使用laravel 5.5

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

我的编辑和删除按钮视图

<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>

我的资源控制器:

namespace App\Http\Controllers;

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

存储工作正常

  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'
];

当我删除按钮时没有任何事情发生并编辑为相同

如果我在第一列添加dd($files)以进行编辑和删除功能,则响应将为[]并且句柄没有错误

这里是我的路线列表

{{3}}

任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:10)

最后2天后我找到了答案,我想在这里为每个可能遇到我问题的人提出答案

对于路由绑定工作,您应该将类​​型提示的变量名称与路径段名称匹配,如需要的文档:

例如我的编辑功能:

这是我编辑的路径URI

admin/file/{file}/edit

你可以看到{file}参数或你打电话的任何内容 现在只需要在函数参数

中写出完全$文件
 public function edit(Files $file)
{
   return view('admin.edit',compact('file'));
}

抱歉,如果我的英语不好

答案 1 :(得分:4)

我再次偶然发现了这个。我不确定这是我自己的错误还是默认缺少这个中间件。

我使用 Laravel 8 并开发了一个 API。当我尝试调用应该隐式绑定到模型的路由时,我总是得到一个空数组作为响应。

TL;DR 检查 NULL 文件 EOF api 数组是否添加了 $ ./bin/firstsecondword press [Enter] on empty line when done. Enter two words: hello Enter second word: world First word is hello Second word is world Enter two words: hello world First word is hello Second word is world Enter two words: 。否则您的请求将无法正确解决。

对 API 的请求 |通过 ID 获取用户 |前端,Vue.js

Kernel.php

$routedMiddlewareGroups

以下是我设置 API 保护路由的方法:

路线/SubstituteBindings::class

UserComponent.vue

methods: { close() { $('#user-modify-modal').modal('hide'); }, open() { $('#user-modify-modal').modal({backdrop: 'static', keyboard: false}); this.fetchUserData(this.params.id); }, async fetchUserData(id) { await axios .get('users/${this.params.id}') .then((result) => { this.user = result.data; }).catch((err) => { console.error(err); }); } },

api.php

没有这个中间件 //protected routes | API Route::group(['middleware' => ['api', 'cors', 'json.response', 'auth:api'], 'prefix' => 'v1'], function() { Route::post('/logout', [ApiLoginController::class, 'logout']); Route::get('/user', function (Request $request) { return $request->user(); }); Route::get('/users', [UserController::class, 'index']); //the function im Testing... Route::get('users/{user}', [UserController::class, 'show']); Route::post('/users', [UserController::class, 'store']); Route::put('/users/{user}', [UserController::class, 'update']); Route::delete('/users/{user}', [UserController::class, 'show']); }); ,隐式绑定将无法工作!

请务必检查您的内核/api 设置

UserController.php

...
/**
     * Get user by ID
     *
     * @param Request $request
     * @param User $user
     * @return void
     */
    public function show(User $user) {

        return response()->json($user);
    }
...

现在打电话时

SubstituteBinding::class

它返回正确的用户数据。

答案 2 :(得分:1)

我知道这个问题已经有解决方案,但是让我添加一些可能对其他人有用的知识...

正如the documentation所说,当使用资源控制器时,资源路由将使用基于资源“单数”名称的参数。对于问题,由于@siros在路由中使用“文件”资源名称(单数形式),因此控制器方法中的绑定名称应为“文件”,尽管他的模型名为Files。如果他尝试使用:

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

由于Files $filefile的单数形式,因此控制器仍然需要files才能工作。

但是,还有其他(更优雅的)解决方案。您可以通过在路由的配置中提供一个parameters选项来更改URL中的带类型变量,如in the documentation所示,该选项将自动应用于show,{{1 }},editupdate方法。例如,这将使您在控制器中使变量名称与模型名称匹配。

因此,在这种情况下,@ siros可以在路由文件中使用它:

destroy

他可以在控制器中使用它:

Route::resource('admin/file','AdminController', [
    'parameters' => [
        'file' => 'files'
    ]
]);

希望这对某人有帮助。

相关问题