【问题标题】:Laravel won't resolve modelLaravel 无法解析模型
【发布时间】:2020-01-11 11:19:09
【问题描述】:

我正在构建一个应用程序来跟踪费用,使用 laravel 5.8 作为后端。 当我尝试访问控制器上的 show 方法时,我发现 laravel 给了我一个新的模型实例,而不是获取与提供的 id 匹配的模型。

我尝试检查从 URI 获取的实际值,这是正确的,并且还手动查询以查看它是否有任何结果,它确实做到了。 我还检查了所有正确书写的名称(检查复数和单数)。

return Expense::all() 的索引方法工作得很好。

模型是空的,它只有一个$guarded 属性。

这是位于 routes/api.php

的路由文件
Route::apiResource('expenses', 'ExpenseController');

这是位于 app/Http/Controllers/ExpenseController.php 的控制器

namespace App\Http\Controllers;

use App\Models\Expense;

class ExpenseController extends Controller
{
    public function show(Expense $expense)
    {
        return Expense:findOrFail($expense->getKey()); // key is null since it's a fresh model
    }
}
public function show($expense)
{
    //dd($expense); //Shows the id given on the URI
    return Expense::where('id', $expense)->firstOrFail(); //Works!
}

费用模型

namespace App\Models;

use App\Model;

class Expense extends Model
{
    protected $guarded = [
        'id'
    ];
}

我希望获得具有给定 ID 的模型的 JSON 数据,但我得到的是带有 $exists = false 的新模型,没有收到任何 404 错误。

我也在使用 laravel/telescope,它显示请求以 200 代码完成,并且没有进行任何查询。

使用dd时的响应

Expense {#378 ▼
  #guarded: array:1 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
}

这是整个类app\Model.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model as BaseModel;

/**
 * Class Model
 * @package App
 * @method static static|null find($id)
 * @method static static findOrFail($id)
 * @method static static create(array $data)
 * @method static Collection all()
 */
class Model extends BaseModel
{

}

修复:我在 RouteServiceProvider 中缺少 Web 中间件

【问题讨论】:

  • 如果您使用Expense $expense,则无需查询Expense;你已经通过$expense...
  • 您是否尝试过访问/api/expenses/1 并确保您的数据库中至少有一条 Expense 记录?
  • 两种显示方法都在控制器中。第一个使用路由模型绑定show(Expense $expense),第二个不使用show($expense)。他们不在同一时间,只是尝试不同的事情
  • @CaddyDZ 是的,这就是我从那个 URI 得到的结果。使用 where 方法查询数据库就可以了。
  • 你可以试试运行php artisan route:list看看有什么问题吗?

标签: php laravel laravel-5.8 route-model-binding


【解决方案1】:

由于我使用 routes/api.php 文件作为路由,我需要将 api 中间件更改为 web位于 app/Providers 内的 RouteServiceProvider 文件中的一个。

需要修改的一段代码:

/**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             //->middleware('api')
             ->middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

【讨论】:

  • 检查app/Http/Kernel.php文件中$middlewareGroupsapi键中的'bindings'值。
猜你喜欢
  • 1970-01-01
  • 2016-12-29
  • 2015-12-23
  • 2019-01-24
  • 1970-01-01
  • 2015-06-17
  • 2023-03-21
  • 2016-02-03
  • 2020-10-22
相关资源
最近更新 更多