2016-01-28 28 views
6

Tôi đang cố lưu một dạng động trong laravel. không thể lưu mảng dữ liệu vào cơ sở dữ liệu. sau đây là biểu mẫu. enter image description hereLaravel lưu mảng dữ liệu đầu vào động vào cơ sở dữ liệu

lĩnh vực hình thức bình thường trong ngắn hạn

{!! 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... 

lĩnh vực hình thức động

{!! 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']) !!} 

tất cả những lĩnh vực đang trong cùng một mẫu. Tôi đang lưu các dữ liệu này vào ba bảng khác nhau.

sau đây là các mẫu tôi đã tạo.
mô hình Ước tính bảng dự toán

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

mô hình EstimateDetail cho bảng estimate_details

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

mục mô hình cho bảng mục

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 do mã là 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'); 
    } 

tôi thành công có thể tiết kiệm xe và ước tính dữ liệu trên bảng xe và dự toán. nhưng tôi không thể lưu các chi tiết ước tính mảng trên bảng estim_details. đã thử nhiều cách khác nhau nhưng thất bại cuối cùng. hãy giúp !!!

Trả lời

3

Tôi đoán ước tính id là mất tích. Và bạn cũng không thể chèn mảng như thế này. Hãy thử mục này cho một mục:

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

Hy vọng điều này có thể hữu ích.

+0

Cảm ơn bạn đã trả lời. Tôi đã thử điều này trước đây. nhưng tôi nhận được lỗi này. "preg_replace(): Tham số không khớp, mẫu là một chuỗi trong khi thay thế là một mảng" – Dhananjaya

+1

Có bởi vì bạn đang cố gắng chèn mảng cùng một lúc.thêm chỉ mục sau các mục của bạn như $ estim_detail-> item_id = $ input ['item_id'] [0]; $ estim_detail-> item_description = $ input ['item_description'] [0]; $ estim_detail-> units = $ input ['units'] [0]; $ estim_detail-> rate = $ input ['rate'] [0]; $ estim_detail-> initial_amount = $ input ['amount'] [0]; nếu nó hoạt động sau đó đi cho một vòng lặp. –

+0

tôi đã thử thêm các chỉ mục không có vòng lặp. vẫn gặp lỗi tương tự. "preg_replace(): Parameter mismatch..." và lỗi chi tiết như sau "'chèn vào' Estimated_details' ('estim_id',' item_id', 'item_description',' units', 'rate',' initial_amount', 'updated_at Giá trị ',' created_at') (89,?,?,?,?,?,?,?) " – Dhananjaya

0

Để lưu trữ estimate detail từ nhiều hàng, Bạn có thể sử dụng phương pháp sau.

// Chuẩn bị mảng của tất cả các dữ liệu sử dụng cho vòng lặp

$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

Cảm ơn bạn đã trả lời. tôi đã thử với cả Eloquent và Query Builder "EstimateDetail :: insert ($ data);" và "DB :: table ('estim_details') -> insert ($ data);" Nhưng tôi nhận được "Undefined offset: 0". bất kỳ ý tưởng nào tại sao điều này xảy ra. – Dhananjaya

+0

Trước khi chèn, hãy xác minh mảng '$ data' của bạn không phải là rỗng và kiểm tra vòng lặp tạo mảng đó. Suy nghĩ chính là tạo ra mảng thích hợp từ dữ liệu đầu vào. – Ali

Các vấn đề liên quan