【问题标题】:Getting ID from url and using it in CRUD controller laravel从 url 获取 ID 并在 CRUD 控制器 laravel 中使用它
【发布时间】:2018-09-12 05:36:55
【问题描述】:

我需要向现有客户添加订单。在我的 customers.show 视图中,我使用以下内容访问订单。

href="{{action('OrderController@create', $customer['id'])}}

页面链接如下:

/orders/create?2f10f8a0-30b9-11e8-8711-dd477b141226

我一直在使用以下 route::get 无法在我的资源控制器中将此 ID 用于订单

Route::get('orders/create/{id}',  'OrderController@create');

基本上需要能够执行以下操作,以便能够在显示创建订单表格的同时显示客户详细信息,但被困在为当前客户传递 id 的最佳方式上

public function create(){
      $customer = Customer::find($id)
      return view('orders.create', compact('customer'));
}

【问题讨论】:

    标签: laravel get routes


    【解决方案1】:

    使用 Laravel,您只需将路由中指定的参数添加为函数参数,如下所示:

    public function create($id){
          $customer = Customer::find($id)
          return view('orders.create', compact('customer'));
    }
    

    【讨论】:

    • 我收到一个错误。 App\Http\Controllers\OrderController::create() 缺少参数 1
    • {id} 是否仍在您的路由定义中?
    • 发现问题。我的路线定义不正确。已使用 Route::get('orders/{id}/create/', 'OrderController@create'); 修复
    【解决方案2】:

    您没有在 create 函数中传递 $id 参数。

    改成:

    public function create( $id ){
        $customer = Customer::find($id)
        return view('orders.create', compact('customer'));
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2021-09-01
      • 2013-12-15
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多