У меня есть модель с именем PriceList, и когда я использую функцию маршрутизации ::resource, она работает, только если я передаю price/lists в качестве первого параметра.
Например, если я сделаю следующее:
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 [▶]
}
Однако, если я изменю ресурс на:
Route::resource('price/lists', 'PriceListsController');
И посетите URL-адрес: /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);
@ceejayoz Спасибо. Я просмотрел список маршрутов и обнаружил проблему. Я использовал pricelists в функции ::model вместо pricelist.






Я посмотрел на php artisan route:list и нашел следующий маршрут:
|GET|HEAD| api/pricelists/{pricelist}|pricelists.show|App\Http\Controllers\PriceListsController@show|web,auth|
Итак, я изменил свой RouteServiceProdiver на:
Route::model('pricelist', PriceList::class);
Вы смотрели
php artisan route:list?