2016-01-28 12 views
6

में गतिशील इनपुट डेटा सरणी को सहेज रहा है मैं लार्वेल में एक गतिशील रूप को सहेजने की कोशिश कर रहा हूं। डेटा बेस में डेटा सरणी को सहेजने में असमर्थ। निम्नलिखित रूप है। enter image description hereलार्वेल डेटाबेस

कम

{!! Form::select('customer_id', ['' => 'Select a customer'] + $customer_list ,null , array('class' => 'form-control', 'id' => 'customer')) !!} 
{!! Form::text('mileage_in', null, ['class' => 'form-control', 'placeholder' => 'Mileage In']) !!} 
. 
. 
.etc... 

गतिशील प्रपत्र फ़ील्ड्स

{!! Form::select('item_id[][item_id]', $items, null, array('class' => 'form-control')) !!} 
{!! Form::text('item_description[][item_description]', null, ['class' => 'form-control', 'placeholder' => 'Not Required | Optional']) !!} 
{!! Form::text('units[][units]', null, ['class' => 'form-control', 'placeholder' => 'Add Units']) !!} 
{!! Form::text('rate[][rate]', null, ['class' => 'form-control', 'placeholder' => 'Add Rate']) !!} 
{!! Form::text('amount[][amount]', null, ['class' => 'form-control', 'placeholder' => 'Add Hrs and Rate', 'id' => 'amount']) !!} 

उन सभी क्षेत्रों में सामान्य प्रपत्र फ़ील्ड्स उसी रूप में कर रहे हैं। मैं इन आंकड़ों को तीन अलग-अलग तालिकाओं में सहेज रहा हूं।

निम्नलिखित मॉडल हैं जिन्हें मैंने बनाया है।
अनुमान तालिका के लिए अनुमान मॉडल

class Estimate extends Model 
{ 
    protected $fillable = [ 
     'customer_id', 
     'vehicle_id', 
     'mileage_in', 
     'net_amount', 
     'parent_estimate_id', 
     'department', 
     'created_by' 
    ]; 
    public function customer(){ 
     return $this->belongsTo('App\Customer'); 
    } 
    public function vehicle(){ 
     return $this->belongsTo('App\Vehicle'); 
    } 
    public function estimate_details(){ 
     return $this->hasMany('App\EstimateDetail'); 
    } 
} 

estimate_details तालिका के लिए EstimateDetail मॉडल आइटम तालिका के लिए

class EstimateDetail extends Model 
{ 
    protected $fillable = [ 
     'estimate_id', 
     'item_id', 
     'item_description', 
     'units', 
     'rate', 
     'labor_amount_final', 
     'initial_amount', 
     'task_status' 
    ]; 
    public function item() 
    { 
     return $this->hasMany('App\Item'); 
    } 
    public function estimate() 
    { 
     return $this->belongsTo('App\Estimate'); 
    } 
} 

आइटम मॉडल

class Item extends Model 
{ 
    protected $fillable = [ 
     'name', 
     'type', 
     'location', 
     'quantity', 
     'sale_price', 
     'unit_of_sale', 
     'pre_order_level', 
     'created_by', 
     'category_id', 
     'service_only_cost', 
     'updated_at' 
    ]; 
    public function estimate_details() 
    { 
     return $this->hasMany('App\EstimateDetail'); 
    } 
} 

Foll कारण कोड EstimatesController

class EstimatesController extends Controller 
{ 
public function store(EstimateRequest $request) 
    { 
     $user_id = '1'; 

     $input = $request->all(); 

     $vehicle = new Vehicle(); 
     $vehicle->customer_id = $input['customer_id']; 
     $vehicle->reg_no = $input['reg_no']; 
     $vehicle->make = $input['make']; 
     $vehicle->model = $input['model']; 
     $vehicle->created_by = $user_id; 

     $estimate = new Estimate(); 
     $estimate->customer_id = $input['customer_id']; 
     $estimate->vehicle_id = $input['vehicle_id']; 
     $estimate->mileage_in = $input['mileage_in']; 
     $estimate->department = $input['department']; 
     $estimate->created_by = $user_id; 

     $estimate_detail = new EstimateDetail(); 
     $estimate_detail->item_id = $input['item_id']; 
     $estimate_detail->item_description = $input['item_description']; 
     $estimate_detail->units = $input['units']; 
     $estimate_detail->rate = $input['rate']; 
     $estimate_detail->initial_amount = $input['amount']; 

     $vehicle->save($request->all()); 
     $vehicle->estimate()->save($estimate); 
     $estimate->estimate_details()->save($estimate_detail); 
     } 
     return redirect('/estimates'); 
    } 

मैं सफलतापूर्वक टेबल वाहनों और अनुमानों पर वाहन और अनुमान डेटा को बचा सकता है है। लेकिन मैं estimate_details तालिका पर अनुमान विवरण सरणी को सहेजने में असमर्थ हूं। कई अलग-अलग तरीकों की कोशिश की लेकिन आखिर में असफल रहा। कृपया मदद करें !!!

उत्तर

3

मुझे लगता है कि अनुमान आईडी याद आ रही है के लिए उपयोग कर सभी डेटा की सरणी तैयार करें। और आप इस तरह के सरणी भी नहीं डाल सकते हैं। इसे एक आइटम के लिए आज़माएं:

$vehicle->save($request->all()); 
    $vehicle->estimate()->save($estimate); 
    $estimate_detail->estimate_id = $estimate->id; 
    $estimate->estimate_details()->save($estimate_detail); 

आशा है कि इससे मदद मिल सकती है।

+0

आपके उत्तर के लिए धन्यवाद। मैंने पहले यह कोशिश की। लेकिन मुझे यह त्रुटि मिल रही है। "preg_replace(): पैरामीटर मिस्चैच, पैटर्न एक स्ट्रिंग है जबकि प्रतिस्थापन एक सरणी है" – Dhananjaya

+1

हां इसके कारण आप एक बार में सरणी डालने की कोशिश कर रहे हैं। $ estimate_detail-> item_id = $ इनपुट ['item_id'] [0] जैसी वस्तुओं के बाद एक अनुक्रमणिका जोड़ें; $ estimate_detail-> item_description = $ इनपुट ['item_description'] [0]; $ estimate_detail-> इकाइयों = $ इनपुट ['इकाइयों'] [0]; $ estimate_detail-> दर = $ इनपुट ['दर'] [0]; $ estimate_detail-> प्रारंभिक_माउंट = $ इनपुट ['राशि'] [0]; यदि यह काम करता है तो एक लूप के लिए जाएं। –

+0

मैंने लूप के बिना इंडेक्स जोड़ने की कोशिश की। अभी भी एक ही त्रुटि हो रही है। "preg_replace(): पैरामीटर मिस्चैच ..." और 'estimate_details' ('estimate_id',' item_id', 'item_description',' इकाइयों', 'दर', 'प्रारंभिक_माउंट',' update_at' में डालने के रूप में विस्तृत त्रुटि के रूप में विस्तृत त्रुटि ', 'create_at') मान (89,?,?,?,?,?,?,?)" – Dhananjaya

0

एकाधिक पंक्तियों में estimate detail स्टोर करने के लिए, आप निम्न दृष्टिकोण का उपयोग कर सकते हैं।

// पाश

$data = []; 
for($i=0,$i<count($input['item_id']);$i++) 
{ 
$data[]= array ('item_id'=>$input[$i]['item_id'], 
       'item_description'=>$input[$i]['item_description']); 
       'units'=>$input[$i]['units']); 
       'rate'=>$input[$i]['rate']); 
       'initial_amount'=>$input[$i]['initial_amount']); 
} 

Model::insert($data); // Eloquent 
DB::table('table')->insert($data); // Query Builder 
+0

आपके उत्तर के लिए धन्यवाद। मैंने वाक्प्रचार और क्वेरी बिल्डर दोनों के साथ प्रयास किया "अनुमानित करें :: सम्मिलित करें ($ डेटा);" और "डीबी :: तालिका ('estimate_details') -> सम्मिलित करें ($ डेटा);" लेकिन मुझे "अपरिभाषित ऑफसेट: 0" मिल रहा है। कोई विचार क्यों यह हो रहा है। – Dhananjaya

+0

सम्मिलित करने से पहले, कृपया सत्यापित करें कि आपका '$ डेटा' सरणी शून्य नहीं है और उस सरणी निर्माण लूप को जांचें। मुख्य विचार इनपुट डेटा से उचित सरणी उत्पन्न करना है। – Ali