相关疑难解决方法(0)

自动注入的 Laravel 模型没有属性

我是 Laravel 的新手。我已经为我的一个表创建了一个模型、一个资源控制器和一个路由,我修改了模型类以使用特定的表名,但是 Laravel 5.4 注入的模型对象没有属性,即使相应的记录存在于数据库。这是我采取的步骤。

1)用工匠创建模型。我运行了这个命令:

php artisan make:model Tree
Run Code Online (Sandbox Code Playgroud)

2)按照说明修改Tree模型类,指定具体的表。我必须这样做,因为我的表被命名为tree,而不是 Laravel 根据其内部规则假设的“树”。

/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'tree';
Run Code Online (Sandbox Code Playgroud)

3)创建一个资源控制器,使用此命令使用我的模型

php artisan make:controller CategoryController --resource --model=Tree
Run Code Online (Sandbox Code Playgroud)

4) 添加资源路由 routes/web.php 以将 Web 服务器路径映射到控制器:

Route::resource('categories', 'CategoryController');
Run Code Online (Sandbox Code Playgroud)

5)修改CategoryController的show()方法,将注入的$tree对象进行var_dump。它看起来像这样:

/**
 * Display the specified resource.
 *
 * @param  \App\Tree  $tree
 * @return \Illuminate\Http\Response
 */
public function show(Tree $tree)
{
    // we need to display the children …
Run Code Online (Sandbox Code Playgroud)

php dependency-injection laravel laravel-5 laravel-eloquent

6
推荐指数
1
解决办法
3356
查看次数