2017-10-11 23 views
0

我需要每一個逗號分隔值的存儲到另一個表,這裏爲發貨表中的輸入:Laravel商店逗號分隔值中的每一個到另一個表

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(); 

它是工作,但我需要改變'9999'值從$軌道英格斯,任何幫助將不勝感激,謝謝

回答

0

海蘭兄弟,

嘗試更換您的9999$trackings[$i]

+0

哦,應該已經試過了,我想$追蹤[I] before..without的美元符號....它的工作,謝謝隊友 –

+0

哇,好兄弟。不要忘了標記爲正確的答案,它會讓我開心,可以幫助你:D –

+0

我需要再等7分鐘才能將它標記爲正確答案..很愚蠢的是那個?!! lol –

相關問題