获取laravel中where查询的ID [英] Get id of where query in laravel
问题描述
我想复制订单,并且有一个orderstatus_id
列,它将获取订单状态ID,而复制订单时,我想将订单状态ID从原来的状态更改为特定的ID.
I want to duplicate my orders and there is orderstatus_id
column which will get orders status id, while duplicating order(s) I want to change the order status id from whatever it is to specific id.
我的订单状态为ID为11
的completed
,我要复制它,并且在复制的订单状态为ID为1
的waiting payment
.在此过程中,我想动态获取该ID,因为我不知道将来的ID是否会保持不变.
my order status is completed
with id 11
, I want copy it and in copied order status become waiting payment
which is id 1
. during this process I want to get that id dynamically because i don't know in future id's will stay the same or not.
所以我有这样的代码:
//re-order
public function reorder($id)
{
$status = Orderstatus::where('title', 'Witing Payment')->select('id')->get();
$order = Order::findOrFail($id);
$newOrder = $order->replicate();
$newOrder->ordernu = mt_rand(1000000000, 9999999999);
$newOrder->orderstatus_id = $status;
$newOrder->payment_id = '';
$newOrder->save();
return redirect()->back()
->with('info',
'Order Stored');
}
当我点击复制链接时,出现此错误:
when I hit copy link i get this error:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
我需要修复此行,(我想)
what i need is fix for this line, (I guess)
$status = Orderstatus::where('title', 'Witing Payment')->select('id')->get();
谢谢.
我的订单表的屏幕截图(如您所见,我的所有整数列都可以为空)
Screenshot of my order table (as you see all my integer columns are nullable)
推荐答案
由于某些外键为空,您将收到错误消息.看起来是payment_id
.如果要允许它为空,则需要在迁移中使其为空:
You're getting the error because some foreign key is empty. Looks like it's payment_id
. If you want to allow it to be empty, you need to make it nullable in migration:
$table->string('payment_id')->nullable();
此外,更改此内容:
Orderstatus::where('title', 'Witing Payment')->select('id')->get();
收件人:
Orderstatus::where('title', 'Witing Payment')->value('id');
这篇关于获取laravel中where查询的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!