使用关系提交单个表单时,是否可以同时将数据保存在两个表中?
我有两张表orders
和invoices
。
我的订单模式是这样的:
class Order extends Model
{
protected $fillable = ['company_id','delivery_date',
'product_name','amount','qty','delivery_vehicle'
,'delivery_cost','status','created_by','verified_by'];
}
和我的发票模型是这样的:
class Invoice extends Model
{
protected $fillable = ['company_id','invoice_id', 'product_name'
'qty','total cost']
}
我的控制器是这样的:
public function create(Request $request)
{
$order = Order::create([
Input::get('company_id'),
Input::get('product_name')
Input::get('qty')
Input::get('delivery_date')
Input::get('delivery_cost')
//........
];
$invoice = Invoice::create([
Input::get('company_id'),
Input::get('product_name')
Input::get('qty')
];
return"Data Saved";
}
但我想save using relationship
答案 0 :(得分:0)
您已经在模型中定义了$fillable
,所以现在您可以使用质量分配使其看起来更干净。你的控制器:
public function create(Request $request)
{
$order = Order::create($request->all());
$invoice = Invoice::create($request->all());
return"Data Saved";
}
我希望它有所帮助