我需要将每个逗号分隔值存储到另一个表中,这里是货运表的输入:
kloter = 1,
fl_date = 2017-10-12,
id_date = 2017-10-12
跟踪= WRT246,RTY6788,YTT665< ---每个都将保存到shipment_tracking表
货件型号:
class Shipment extends Model
{
protected $fillable = ['kloter','fl_date','id_date'];
public function trackings()
{
return $this->hasMany('App\ShipmentTracking');
}
}
ShipmentTracking模型:
class ShipmentTracking extends Model
{
protected $fillable = ['shipment_id','track_no'];
public function shipment()
{
return $this->belongsTo('App\Shipment','shipment_id');
}
}
这是我的控制器:
$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',任何帮助都将不胜感激,谢谢
答案 0 :(得分:0)
Hy bro,
尝试将9999
替换为$trackings[$i]