如何更新现有行并在表单laravel上同时存储其他行

ttp71kqs  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(139)

我建立一个工作报价网站作为我在拉雷维尔学习的一部分。该网站允许交易人员添加用于工作的材料。用户在必要的表单字段中添加所有信息,如名称、工作描述等,以添加我使用的可以动态添加新行的滑块。
添加行,然后使用create/store方法提交表单。如果用户决定要编辑表单,则将从数据库中显示任何材质项。我和(quoteitem)belongsto和(quote)hasmany使用了雄辩的关系。我遇到的问题是,如果用户希望添加新行或编辑现有行,如何在submit上存储这些信息。我尝试了updateorcreate方法,但它不允许重复数据,用户可以决定添加相同的项目两次。我可以使用仅附加到现有行的id进行更新,但不能存储任何其他行。有没有办法做到这一点?任何帮助都将不胜感激。

// Quote controller
public function edit(Quote $quote){
        $user = Auth::user();
        $date = Carbon::now()->format('j F Y');
        return view('quotes.edit', compact('quote', 'date', 'user'));
}

public function update(Request $request, Quote $quote){
        $this->validate($request, [
            'description'  => 'max:10000|nullable',
            'quoteVat'     => 'numeric|nullable',
            'discount'     => 'numeric|nullable',
            'subTotal'     => 'numeric|nullable',
            'total'        => 'numeric|nullable',
            'notes'        => 'max:5000|nullable',
            'introduction' => 'max:5000|nullable',
            'view_intro'   => 'in:1|in:0',
            'view_items'   => 'in:1|in:0',
            'view_jobdes'  => 'in:1|in:0'
        ]);

        //insert items from materials slide form
        if($request->has('totalMaterials')){
            //option to get all row entries stored in an array that have a total value
            $totalMaterials = $request->totalMaterials;
            //get all materials with an id
            $request->material_id ?  $material_id = $request->material_id :  $material_id = false;
            if($material_id){
                foreach($material_id as $key => $value){
                    $item = QuoteItem::where('id', $material_id[$key])->first();
                    $data = [
                        'description' => $request->materialItem[$key],
                        'quantity'    =>  $request->quantity[$key],
                        'unit_type'   => '',
                        'item_type'   => 'materials',
                        'price'       =>  $request->priceMaterials[$key],
                        'total'       =>  $request->totalMaterials[$key]
                    ];  
                    $item->update($data);
                }
            }
        }
        return back()->with('success', 'Quote created for '.$quote->job->name);
}

刀片:

<div class="material_slide_price_items">
        <div class="table-responsive" style="border:0;">
            <span id="result"></span>
            <table class="table" id="tableMaterial">
                <thead>
                    <th>Description</th>
                    <th>Quantity</th>
                    <th>Price</th>
                    <th>Total price</th>
                    <th>Action</th>
                </thead>
                <tbody id="product_quote_table">

                    @foreach($quote->quoteItems as $materialItem)
                    <tr>
                        @if($materialItem->item_type == 'materials')
                        <td><input type="text" class="form-control" name="materialItem[]" value="{{ old('materialItem') ?? $materialItem->description }}" placeholder="Enter description"></td>
                        <td><input type="text" class="form-control" name="quantity[]" value="{{ old('unit') ?? $materialItem->quantity }}" placeholder="0"></td>
                        <td><input type="text" class="form-control" name="priceMaterials[]" value="{{ old('unit') ?? $materialItem->price }}"  placeholder="0.00"></td>
                        <td>
                            <input type="text" class="form-control tMaterials" id="totalMaterials" value="{{ number_format( $materialItem->quantity * $materialItem->price, 2) }}" name="totalMaterials[]" readonly placeholder="0.00">
                            {{-- added to update existing rows --}}
                            <input type="hidden" name='material_id[]' value="{{  $materialItem->id ?  $materialItem->id : '' }}">
                        </td>
                        <td><a href="#" id="remove_materials_item_quote" class="text-muted no-border mt-2"><i class="far fa-trash-alt"></i></a></td>
                    </tr>
                    @endif
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题