【发布时间】:2016-12-01 15:02:09
【问题描述】:
我在使用注入 Eloquent 到控制器方法中获取模型时遇到问题 - 它在 URL 中找不到使用 id 的模型。 我记得我在其他控制器中更改了
DB::setFetchMode(PDO::FETCH_ASSOC);
dd($infrastructure) 只返回有关模型的元数据:
Infrastructure {#257 ▼
#table: "infrastructures"
#fillable: array:10 [▶]
#connection: null
#primaryKey: "id"
....
}
我的控制器方法:
public function show(Infrastructure $infrastructure)
{
$card = [];
$card['name'] = $infrastructure->name;
$card['id'] = $infrastructure->id;
$card['type'] = "infrastructure";
$card['items']['manufacturer'] = $infrastructure->manufacturer;
$card['items']['type'] = $infrastructure->infrastructuretype()->first()- >name;
$card['items']['kind'] = $infrastructure->kind()->first()->name;
$card['items']['serial'] = $infrastructure->serial;
$card['items']['more info'] = Crypt::decrypt($infrastructure->long_desc);
$title = 'Infrastructure details ';
$warranty_left = $infrastructure->warranty_date > $infrastructure->purchase_date ? (new Carbon($infrastructure->warranty_date))->diffInDays(new Carbon($infrastructure->purchase_date)) : 0;
return view('infrastructure.show', compact('infrastructure', 'card', 'title'));
}
我的路线:
Route::model('infrastructure', 'App\Infrastructure');
Route::group(['middleware' => 'auth'], function () {
Route::resource('infrastructure', 'InfrastructureController');
get('infrastructure/{infrastructure}/logs', [
'uses' => 'InfrastructureController@showInfrastructureLogs'
]);
resource('infrastructuretype', 'InfrastructureTypeController');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
});
【问题讨论】:
标签: laravel laravel-5.1 laravel-routing