Laravel路由资源

时间:2018-04-20 18:47:07

标签: laravel laravel-5

我有一个名为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);

1 个答案:

答案 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);