我有一个名为PriceList
的模型,当我使用路由的::resource
函数时,它只有在我将price/lists
作为第一个参数传递时才有效。
例如,如果我执行以下操作:
Route::resource('pricelists', 'PriceListsController');
然后在我的控制器内,我执行以下操作:
use App\PriceList;
class PriceListsController
{
public function show(PriceList $list)
{
dd($list);
}
}
如果我访问了网址:/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 [▶]
}
但是,如果我将资源更改为:
Route::resource('price/lists', 'PriceListsController');
访问网址:/price/lists/1
,然后我获得PriceList
的正确实例:
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/lists
更改为pricelists
?
我尝试在Route::model
中使用RouteServiceProvider
函数:
Route::model('pricelists', App\PriceList::class);
答案 0 :(得分:0)
我查看了php artisan route:list
,找到了以下路线:
|GET|HEAD| api/pricelists/{pricelist}|pricelists.show|App\Http\Controllers\PriceListsController@show|web,auth|
所以我已将RouteServiceProdiver
更改为:
Route::model('pricelist', PriceList::class);