2017-10-11 7 views
0

別のテーブルへの出荷表の入力をカンマ区切り値のそれぞれを格納します2017年10月12日、Laravelはここで、Iは、別のテーブルにカンマ区切り値の各々を格納する必要が

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'); 
    } 
} 

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

それが働いているが、私はそれを変更する必要があります$ trackの値を持つ '9999'イングスは、任意の助けが理解されるであろう、おかげ

答えて

0

Hyの仲間、$trackings[$i]

+0

であなたの9999を交換する

試みは男ああ、私は追跡$を試してみました、ことを試みている必要があります[i]はbefore..withoutドル記号...それは働いた、感謝の仲間 –

+0

うわー、大丈夫。正解としてマークするのを忘れないでください、それは私を幸せにすることができます:D –

+0

私は正しい答えとしてマークするために別の7分を待つ必要があります。笑 –

関連する問題