我是 Laravel 的新手。我已经为我的一个表创建了一个模型、一个资源控制器和一个路由,我已经修改了模型类以使用特定的表名,但是 Laravel 5.4 注入的模型对象没有属性,即使对应的记录存在于数据库。这是我采取的步骤。
1) 与工匠一起创建模型。我运行了这个命令:
php artisan make:model Tree
2)按照说明修改Tree模型类,指定具体的表。我必须这样做,因为我的表被命名为tree,而不是 Laravel 根据其内部规则假设的“树”。
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'tree';
3)使用此命令创建一个使用我的模型的资源控制器
php artisan make:controller CategoryController --resource --model=Tree
4) 添加资源路由 routes/web.php 以便将 Web 服务器路径映射到控制器:
Route::resource('categories', 'CategoryController');
5)修改CategoryController的show()方法为var_dump注入的$tree对象。它看起来像这样:
/**
* 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”,但这应该包含在我上面所做的更改中:
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 的记录。
7) 我访问应该激活资源控制器的 show() 方法的 url。我得到的输出证实这实际上是方法 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 语句,我没有创建任何代码。它都是由工匠命令自动生成的,明确按照文档的指示。
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);
}
}