gpt4 book ai didi

php - 检查行是否存在以及是否没有插入数据

转载 作者:行者123 更新时间:2023-11-29 23:56:21 24 4
gpt4 key购买 nike

我有一个 Eloquent 模型,它正在向我的一个表中创建新条目。我遇到的问题是每次页面运行时都会一遍又一遍地插入数据。如何检查该数据是否已存在于该表中,如果不存在,则插入该特定数据?

        foreach($xml as $product) {        

//var_dump($product); die;

Order::where('id', '=', $product->reference)->update(
array(
'invoice_id' => $product['orderid'],
'shipped' => $product['shipped'],
'processed' => $product['processed'],
'problem' => $product['problem'],
)
);

$tracking = new OrderTrack();
$tracking->invoice_id = $product['orderid'];
$tracking->shipper = $product->shipping->shipper;
$tracking->shipping_method = $product->shipping->shipping_method;
$tracking->shipping_date = $product->shipping->shipping_date;
$tracking->shipping_cost = $product->shipping->shipping_cost;
$tracking->tracking_number = $product->shipping->tracking_number;
if(!empty($product->shipping->shipping_notes)) {
$tracking->shipping_notes = $product->shipping->shipping_notes;
}

$tracking->save();
echo'done';
}

OrderTrack 是我关心的代码部分。无论该行是否已存在,它都会创建一个新条目。

$xml var_dump

object(SimpleXMLElement)[294]
public 'order' =>
array (size=15)
0 =>
object(SimpleXMLElement)[295]
public '@attributes' =>
array (size=7)
...
public 'reference' => string '3053' (length=4)
public 'reference2' =>
object(SimpleXMLElement)[310]
...
public 'reference3' =>
object(SimpleXMLElement)[311]
...
public 'invoice' =>
object(SimpleXMLElement)[312]
...
public 'order_date' => string '2014-08-14 03:00:06' (length=19)
public 'billing_date' => string '2014-08-14 00:00:00' (length=19)
public 'changed' => string '2014-08-14 13:02:15' (length=19)
public 'sender' =>
object(SimpleXMLElement)[313]
...
public 'receiver' =>
object(SimpleXMLElement)[314]
...
public 'shipping' =>
object(SimpleXMLElement)[315]
...
public 'products' =>
object(SimpleXMLElement)[316]
...
1 =>
object(SimpleXMLElement)[296]
public '@attributes' =>
array (size=7)
...
public 'reference' => string '3054' (length=4)
public 'reference2' =>
object(SimpleXMLElement)[328]
...
public 'reference3' =>
object(SimpleXMLElement)[329]
...
public 'invoice' =>
object(SimpleXMLElement)[330]
...
public 'order_date' => string '2014-08-13 22:00:02' (length=19)
public 'billing_date' => string '2014-08-14 00:00:00' (length=19)
public 'changed' => string '2014-08-14 13:02:20' (length=19)
public 'sender' =>
object(SimpleXMLElement)[331]
...
public 'receiver' =>
object(SimpleXMLElement)[332]
...
public 'shipping' =>
object(SimpleXMLElement)[333]
...
public 'products' =>
object(SimpleXMLElement)[334]
...

最佳答案

利用了firstOrNew

        $tracking = OrderTrack::firstOrNew(array('invoice_id' => $product['orderid']));

$tracking->invoice_id = $product['orderid'];

$tracking->shipper = $product->shipping->shipper;

$tracking->shipping_method = $product->shipping->shipping_method;

$tracking->shipping_date = $product->shipping->shipping_date;

$tracking->shipping_cost = $product->shipping->shipping_cost;

$tracking->tracking_number = $product->shipping->tracking_number;

if(!empty($product->shipping->shipping_notes)) {
$tracking->shipping_notes = $product->shipping->shipping_notes;
}

$tracking->save();

关于php - 检查行是否存在以及是否没有插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25312764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com