Laravel路由资源
我有一个名为Laravel路由资源,laravel,laravel-5,Laravel,Laravel 5,我有一个名为PriceList的模型,当我使用路由的::resource功能时,它只有在我将price/list作为第一个参数传递时才起作用 例如,如果我执行以下操作: Route::resource('pricelists', 'PriceListsController'); use App\PriceList; class PriceListsController { public function show(PriceList $list) { dd($
PriceList
的模型,当我使用路由的::resource
功能时,它只有在我将price/list
作为第一个参数传递时才起作用
例如,如果我执行以下操作:
Route::resource('pricelists', 'PriceListsController');
use App\PriceList;
class PriceListsController
{
public function show(PriceList $list)
{
dd($list);
}
}
然后在控制器中,我执行以下操作:
Route::resource('pricelists', 'PriceListsController');
use App\PriceList;
class PriceListsController
{
public function show(PriceList $list)
{
dd($list);
}
}
如果我访问url:/pricelists/1
,它会给我一个PriceList
的空实例:
PriceList {#802 ▼
#appends: array:4 [▶]
#hidden: array:4 [▶]
#with: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
PriceList {#820 ▼
#appends: array:4 [▶]
#hidden: array:4 [▶]
#with: array:1 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▶]
#original: array:7 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
但是,如果我将资源更改为:
Route::resource('price/lists', 'PriceListsController');
然后访问url:/price/lists/1
,我就得到了PriceList
的正确实例:
PriceList {#802 ▼
#appends: array:4 [▶]
#hidden: array:4 [▶]
#with: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
PriceList {#820 ▼
#appends: array:4 [▶]
#hidden: array:4 [▶]
#with: array:1 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▶]
#original: array:7 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
如何将其从price/list
更改为pricelists
更新
我已尝试在我的RouteServiceProvider
中使用Route::model
函数:
Route::model('pricelists', App\PriceList::class);
我查看了
php artisan route:list
,发现了以下路径:
|GET|HEAD| api/pricelists/{pricelist}|pricelists.show|App\Http\Controllers\PriceListsController@show|web,auth|
因此,我已将我的RouteServiceProDriver
更改为:
Route::model('pricelist', PriceList::class);
你看过php artisan route:list了吗?@ceejayoz谢谢,我看过了路由列表,发现了问题。我在
::model
函数中使用了pricelists
,而不是pricelist
。