代码之家  ›  专栏  ›  技术社区  ›  Chin Leung

Laravel路由资源

  •  0
  • Chin Leung  · 技术社区  · 8 年前

    我有一个叫 PriceList 当我使用 ::resource 路由功能,只有我通过时才有效 price/lists 作为第一个参数。

    例如,如果我执行以下操作:

    Route::resource('pricelists', 'PriceListsController');
    

    然后在控制器中,我执行以下操作:

    use App\PriceList;
    
    class PriceListsController
    {
        public function show(PriceList $list)
        {
            dd($list);
        }
    }
    

    如果我访问url: /pricelists/1 ,它给了我一个空的 价格表 :

    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 {#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 [▶]
    }
    

    如何更改 价格/清单 pricelists ?

    使现代化

    我试过使用 Route::model my中的函数 RouteServiceProvider :

    Route::model('pricelists', App\PriceList::class);
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Chin Leung    8 年前

    我已经看过了 php artisan route:list ,并找到以下路径:

    |GET|HEAD| api/pricelists/{pricelist}|pricelists.show|App\Http\Controllers\PriceListsController@show|web,auth|
    

    所以我改变了我的 RouteServiceProdiver 收件人:

    Route::model('pricelist', PriceList::class);
    
    推荐文章