我是拉勒维尔的新手。我已经为我的一个表创建了一个模型、一个资源控制器和一个路由,我修改了模型类以使用特定的表名,但是Laravel 5.4注入的模型对象没有属性,即使数据库中存在相应的记录。这是我所采取的步骤。
1)与工匠一起创建模型。我执行了这个命令:
php artisan make:model Tree2)将Tree类修改为受命,以指定特定的表。我不得不这样做,因为我的表名为tree,而不是Laravel根据其内部规则假设的“树”。
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'tree';3)使用以下命令创建一个使用我的模型的资源控制器
php artisan make:controller CategoryController --resource --model=Tree4)添加资源路由路由/web.php,以便将web服务器路径映射到控制器上:
Route::resource('categories', 'CategoryController');5)修改 CategoryController ()CategoryController的方法,使注入的$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 of $tree
var_dump($tree);
}6)我的表结构遵循Laravel的所有约定( 规定 )。有一个整数id列,它是无符号的&自动递增。我有created_at和updated_at时间戳。唯一不同的地方是表名是"tree“,而不是”tree“,但应该用我上面所做的更改来说明:
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。我得到的输出验证了这个方法实际上是::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);
}
}发布于 2017-03-01 22:39:35
有一个关于路由模型绑定的命名约定。
尝试将操作调用更改为:
public function show(Tree $category)
{
var_dump($category);
}更新:我查看了源代码,您还可以在资源路由声明中更改参数名称:
Route::resource('categories', 'CategoryController', ['parameters'=>['categories'=>'tree']]);并在操作调用中使用$tree变量。
public function show(Tree $tree)https://stackoverflow.com/questions/42543124
复制相似问题