自动注入的Laravel模型没有属性 [英] auto-injected Laravel model has no attributes

查看:68
本文介绍了自动注入的Laravel模型没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Laravel. I have created a model, a resource controller, and a route for one of my tables, I have modified the model class to use a particular table name, but the model object injected by Laravel 5.4 has no attributes even though a corresponding record exists in the database. Here are the steps I took.

1)使用工匠创建模型.我运行了以下命令:

1) Create the model with artisan. I ran this command:

php artisan make:model Tree

2)将Tree模型类修改为指示,以指定特定的桌子.我之所以必须这样做,是因为我的表名为 tree ,而不是Laravel根据其内部规则假定的树".

2) Modify the Tree model class as instructed to specify a specific table. I had to do this because my table is named tree, not the "trees" as Laravel would otherwise assume based on its internal rules.

/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'tree';

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

3) Create a resource controller that makes use of my model with this command

php artisan make:controller CategoryController --resource --model=Tree

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

4) Add a resource route routes/web.php in order to map a web server path onto the controller:

Route::resource('categories', 'CategoryController');

5)修改CategoryController的 show()方法,以var_dump注入的$ tree对象.看起来像这样:

5) Modify the show() method of the CategoryController to var_dump the injected $tree object. It looks like this:

/**
 * Display the specified resource.
 *
 * @param  \App\Tree  $tree
 * @return \Illuminate\Http\Response
 */
public function show(Tree $tree)
{
    // we need to display the children of $tree
    var_dump($tree);

}

6)我的表结构遵循由Laravel文档指定的所有约定 .整数 id 列是未签名的& ;;自动递增.我有created_at和Updated_at时间戳.唯一不同的是表名是"tree"而不是"trees",但这应该包含在我上面所做的更改中:

6) My table structure follows all the conventions specified by Laravel docs. There is an integer id column that is unsigned & auto-incrementing. I have the created_at and updated_at timestamps. The only thing different is that the table name is "tree" and not "trees", but that should be covered with the change I made above:

CREATE TABLE IF NOT EXISTS `tree` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) unsigned DEFAULT NULL,
  `label` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `display_order` int(11) unsigned NOT NULL DEFAULT '0',
  `forum_id` int(5) NOT NULL DEFAULT '0',
  `url` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `flavor` tinyint(4) NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `parent_pkey` (`parent_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

此表包含数据. 最肯定有一条ID = 1的记录.

This table contains data. It most definitely has a record with id=1.

7)我访问了应该激活资源控制器的show()方法的URL.我得到的输出证实这实际上是方法CategoryController :: show(). http://example.com/categories/1

7) I visit the url which should activate the show() method of my resource controller. The output I get verfies that this is in fact the method CategoryController::show(). http://example.com/categories/1

这是问题所在.

var_dump($ tree)的输出没有属性.没有错误,但是注入的对象有问题.

object(App\Tree)#217 (24) {
  ["table":protected]=>
  string(4) "tree"
  ["connection":protected]=>
  NULL
  ["primaryKey":protected]=>
  string(2) "id"
  ["keyType":protected]=>
  string(3) "int"
  ["incrementing"]=>
  bool(true)
  ["with":protected]=>
  array(0) {
  }
  ["perPage":protected]=>
  int(15)
  ["exists"]=>
  bool(false)
  ["wasRecentlyCreated"]=>
  bool(false)
  ["attributes":protected]=>
  array(0) {
  }
  ["original":protected]=>
  array(0) {
  }
  ["casts":protected]=>
  array(0) {
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["appends":protected]=>
  array(0) {
  }
  ["events":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["relations":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["timestamps"]=>
  bool(true)
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["fillable":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
}

我做错了什么吗?如何让Laravel注入正确的物体?

有人问为什么我在路线中注入了错误的物体.这是在步骤#3中自动生成的类的缩写版本.很明显,它引用了Tree类并期望得到树对象的代码提示.除了var_dump语句外,我没有创建任何此类代码.完全由工匠命令自动生成,如文档所指示.

Someone asked why I was injecting the wrong object in my route. Here is an abbreviated version of the class that was auto-generated in step #3. It clear references the Tree class and code-hints it is expecting a tree object. I did not create any of this code except the var_dump statement. It was all auto-generated by artisan commands expressly as instructed by the docs.

namespace App\Http\Controllers;

use App\Tree;
use Illuminate\Http\Request;

class CategoryController extends Controller
{

    /**
     * Display the specified resource.
     *
     * @param  \App\Tree  $tree
     * @return \Illuminate\Http\Response
     */
    public function show(Tree $tree)
    {
        // we need to display the children of $tree
        var_dump($tree);

    }

}

推荐答案

路由模型绑定有一个命名约定.

There is a naming convention on Route Model binding.

尝试将操作调用更改为此:

Try to change the action call to this:

public function show(Tree $category)
{
    var_dump($category);
}

更新: 我查看了源代码,还可以在资源Route声明中更改参数名称:

Update: I looked in the source and you can also change the parameters name in resource Route declaration:

Route::resource('categories', 'CategoryController', ['parameters'=>['categories'=>'tree']]);

并在操作调用中使用$ tree变量

And use the $tree variable in action call

public function show(Tree $tree)

这篇关于自动注入的Laravel模型没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆