从URL获取ID并在CRUD控制器laravel中使用它

时间:2018-04-03 01:14:23

标签: laravel get routes

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

function DeploySchedule(){
    $.ajax({
        type: "POST"
        , url: "/Services/ScheduleService.svc/DeploySchedule"
        , contentType: "application/json; charset=utf-8"
        , dataType: "json"
        , success: function (result) {
            $('#calendar').fullCalendar('removeEvents');

            result.forEach(function (_row) {
                var event = new Object();
                event.title = _row.TrackingNumber + " (" + _row.Worker + ")";
                event.start = _row.Date;
                event.WorkerGuid = _row.WorkerGuid;
                event.ScheduleGuid = _row.ScheduleGuid;
                event.TrackingNumber = _row.TrackingNumber;
                event.Worker = _row.Worker;
                $('#calendar').fullCalendar('renderEvent', event);
            });
        }
        , error: function (xhr, status, error) { // if error occure
            alert(xhr.responseText);
        }
        , complete: function () {
        }
    });
}

页面的链接如下:

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

我一直在使用以下路线::无法在我的资源控制器中为订单使用此ID

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

基本上需要能够在下面的内容中显示创建订单表单时能够显示客户详细信息,但是最好的方法是将ID传递给当前客户

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

将其更改为:

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