【发布时间】:2015-04-24 13:43:45
【问题描述】:
我对 Laravel 5 路由模型绑定有疑问 我正在使用以下控制器方法
public function destroy(PrimaryLocation $primaryLocation) {
dd($primaryLocation->id);
$primaryLocation->delete();
return redirect()->back()->with('locationDeleted', true);
}
PrimaryLocation 是一个 Eloquent 模型
我的RouteServiceProvider的开机功能:
public function boot(Router $router)
{
parent::boot($router);
$router->model('user', 'App\User');
$router->model('PrimaryLocation', 'App\PrimaryLocation');
}
在我的 routes.php 中
Route::delete('deletePrimaryLocation/{PrimaryLocation}',
['as' => 'admin.deletePrimaryLocation', 'uses' => 'LocationsController@destroy']);
此设置在我的本地计算机上运行良好,但是当我将文件部署到我的开发服务器时,模型绑定会中断; 执行该方法时,位置不会被删除。
我做了一些 var_dumps
dd($primaryLocation->id);
在本地计算机上这会返回正确的 id,但在服务器上它会 只返回null;
但是如果我做一个
dd($primaryLocation)
结果在本地
PrimaryLocation {#178 ▼
#fillable: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:4 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
在我的服务器上几乎相同...但缺少属性:
PrimaryLocation {#195 ▼
#fillable: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
}
有人知道可能出了什么问题吗?
[更新]
如果我注释掉
// $router->model('PrimaryLocation', 'App\PrimaryLocation');
在我的 ServiceProvider 中,本地行为与服务器上的相同。 加载 ServiceProvider 可能有问题?也许有某种缓存?
【问题讨论】:
-
缺少
attributes是因为找不到记录。数据库中的数据是否相同?即相同的数据库、表和字段?具体来说——记录本身是否存在? -
您的文件名在服务器上是否正确?是
PrimaryLocation.php区分大小写一样吗? -
如果找不到记录,应该有 404 错误(至少文档说明了这一点),如果我是正确的。如果文件名不正确,则应该是某种“硬”错误...(哎呀,出了点问题)
-
是的 - 正确 - 但我只是想排除各种问题。
-
只是为了澄清 - 问题是没有找到记录。我不知道为什么它没有抛出 404 - 但你得到
null为id的事实证明了这一点。另外,当您dd()时,您可以清楚地看到exists: false的对象。所以我们需要确认记录是实际上在数据库中 - 然后从那里开始。