laravel-5 - 将多个表 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);
}
解决方案
您应该使用 eloquent 关系您可以根据您的表关系使用此文档 [mant to many relationship DOC]
并使用此关系来获取客户的预订:
Customer::with('booking')->find($customer_id);
*booking 是客户模型中的关系名称
推荐阅读
- vb.net - Vb数学计算不正确的结果
- ios - Swift - 获得一对多的关系
- php - 为给定顶级类别的所有子类别禁用特定的 WooCommerce 付款方式
- bash - .bashrc 命令正在回显到终端。我能阻止这个吗
- c++ - vscode c ++中的分段错误
- r - 根据不同的列重新编码列中的观察
- android - 有没有办法修复错误:当我尝试在 Visual Studio 上将 Framework 升级到 Android 10.0 Q 时出现 AMM0000
- android - SharedPreferences 未在 onCreate() 中加载
- asp.net-core - 需要确认电子邮件才能在 IdentityServer4 上使用 OAuth2 登录
- c# - 如何使用 LINQ 按属性获取重复项