gpt4 book ai didi

php - Laravel 将每个逗号分隔值存储到另一个表中

转载 作者:行者123 更新时间:2023-11-30 21:52:22 25 4
gpt4 key购买 nike

我需要将每个逗号分隔值存储到另一个表中,这里是 shipments 表的输入:

克洛特 = 1 ,

fl_date = 2017-10-12,

id_date = 2017-10-12

trackings = WRT246、RTY6788、YTT665 <--- 这些都将保存到 shipment_tracking 表中

装运模式:

class Shipment extends Model
{
protected $fillable = ['kloter','fl_date','id_date'];

public function trackings()
{
return $this->hasMany('App\ShipmentTracking');
}
}

cargo 跟踪模型:

class ShipmentTracking extends Model
{
protected $fillable = ['shipment_id','track_no'];

public function shipment()
{
return $this->belongsTo('App\Shipment','shipment_id');
}
}

这是我的 Controller :

$shipments = new Shipment();

$shipments->kloter = $request->input('kloter');
$shipments->fl_date = $request->input('fl_date');
$shipments->id_date = $request->input('id_date');


$shipments->save();

$lastshipment = $shipments->id; // find the last inserted ID

$trackings = explode(',', request('trackings')); // explode comma separated values from trackings

$cnt=count($trackings); // count the numbers of trackings

$i=0;

for($i=0;$i<$cnt;$i++) //iteration

//i am using raw query
// code bellow will save the ID of shipment to shipment_tracking,
//but i need to change that 9999 value with each values from $trackings
//i dont know what to put in that '9999'
{
DB::table('shipment_trackings')->insert(
array(
'shipment_id' => $lastshipment,
'track_no' => '9999' // just a random value
)
);
}

// Shipment::find($lastshipment)->trackings()->associate($trackings);



return back();

它正在工作,但我需要用 $trackings 中的值更改“9999”,我们将不胜感激任何帮助,谢谢

最佳答案

兄弟,

尝试用 $trackings[$i] 替换你的 9999

关于php - Laravel 将每个逗号分隔值存储到另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689390/

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