首页 > 解决方案 > 将多个表 id 传递给视图 Laravel

问题描述

所以这是我的问题。我正在尝试为两个表 Customer 和 Booking 创建两个条目。其中客户 ID 是预订表中的外键和客户表中的主键。我想要做的是将新生成的客户 id 和预订 id 传递给同一控制器中的另一个函数(编辑函数)。这样我就可以用生成的客户 id 更新预订表列。有没有简单的方法来做这些事情。

        $booking =new Booking;
        $customer=new Customer;
        $booking->piklocation=$request->input('piklocation');
        $booking->date=$request->input('date');
        $customer=new Customer;
        $booking->save();
        $customer->save();
        return redirect()->route('booking.edit', $booking->id,$customer->id);
    }


}
/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id,$id2)
{
    //Validation of the edit code
    $booking = Booking::find($id);
    $customer=Customer::find($id2);
    return view('pages.vehicleselect')->with('booking',$booking)->with('customer',$customer);

}

标签: laravel-5model-view-controller

解决方案


您应该使用 eloquent 关系您可以根据您的表关系使用此文档 [mant to many relationship DOC]

并使用此关系来获取客户的预订:

Customer::with('booking')->find($customer_id);

*booking 是客户模型中的关系名称


推荐阅读