laravelに動的フォームを保存しようとしています。データ配列をデータベースに保存できません。以下はフォームです。短いLaravel動的入力データ配列をデータベースに保存する
{!! 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']) !!}
通常フォームフィールドが同じ形式です。 これらのデータを3つの異なるテーブルに保存しています。
以下は私が作成したモデルです。推定テーブルの
推定モデル
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テーブルの推定詳細配列を保存することができません。いろいろな方法で試しましたが、やがて失敗しました助けてください!
お返事ありがとうございます。私はこれを前に試しました。しかし、私はこのエラーが発生しています。 "preg_replace():パラメータが一致していません。パターンは文字列であり、置換は配列です" – Dhananjaya
はい、あなたはすぐに配列を挿入しようとしています。[0] [ 'ITEM_ID'] $ estimate_detail->のitem_id = $入力のようなあなたの項目の後にインデックスを追加します。 $ estimate_detail-> ITEM_DESCRIPTION = $入力[ 'ITEM_DESCRIPTION'] [0]。 $ estimate_detail-> units = $ input ['units'] [0]; $ estimate_detail-> rate = $ input ['rate'] [0]; $ estimate_detail-> initial_amount = $入力[ '量'] [0]。 それは、ループのために行く動作するかどうか。 –
ループなしでインデックスを追加しようとしました。まだ同じエラーが発生しています。 'estimate_details'(' estimate_id'、 'item_id'、' item_description'、 'units'、' rate'、 'initial_amount'、' updated_atの挿入」」次のようにして、詳細なエラー: "パラメータの不一致にpreg_replace()。。。" '、' created_at')の値(89、?、?、?、?、?、?、?)」 – Dhananjaya