0

我想在 Laravel 的一个 delivery_notes_id 中插入更多项目。但我收到错误“数组到字符串的转换”。当我用 $lastid 删除代码时,没有错误。你能帮助我吗?

$data=$request->all();
        $lastid=delivery_note::create($data)->id;
        $delivery_note->datum = $data['datum'];
        $delivery_note->customer_id = $data['customer'];

        if(count($data['produkt']) > 0)
        {
            $data2=[];
            foreach($data['produkt'] as $item => $value)
                array_push($data2, [
                    'delivery_notes_id'=>$lastid,
                    'menge'=>$request['menge'][$item],
                    'einheit'=>$request['einheit'][$item],
                    'product_id'=>$request['produkt'][$item],
                ]);
                Items::insert($data2);
            }
       return $this->index()->with([
        'meldung_success' => 'Lieferschein wurde erstellt!'
    ]);
}

这是 HTML 代码。我已经尝试过以下教程https://tsdurjoy.blogspot.com/2019/01/laravel-multiple-data-insert-into_20.html

<div class="container">
<div class="row justify-content-center">
    <div class="col-md-12">
        <div class="container">
            <h4 class="text-center"><a href="/delivery_note"><i class="fas fa-chevron-left"></i></a> neuer LIEFERSCHEIN</h4>

            <form class="row g-3" action="/delivery_note" method="post">
                @csrf
                <table>
                    <tbody>
                        <tr>
                            <label for="datum" class="form-label">Datum</label>
                            <input type="date" class="form-control" name="datum" id="datum" value="{{ old('datum') }}">
                        </tr>

                        <tr>
                            <label for="customer_id" class="form-label">Kunde</label>
                            <select id="customer_id" class="form-control" name="customer">
                                <option selected>Kunde wählen</option>
                                @foreach (App\Customer::get() as $customers)
                                    <option value="{{$customers->id}} ">{!! $customers->customer !!}</option>
                                @endforeach
                            </select>
                         </tr>

                        <div id="more">
                            <tr>
                                <td>
                                    <label for="menge" class="form-label">Menge</label>
                                    <input type="text" class="form-control" name="menge[]" id="menge" value="{{ old('menge') }}">
                                </td>
                                <td>
                                    <label for="einheit" class="form-label">EH</label>
                                    <input type="text" class="form-control" name="einheit[]" id="einheit" value="{{ old('einheit') }}">
                                </td>
                                <td>
                                    <label for="produkt" class="form-label">Produkt</label>
                                    <select id="produkt" class="form-control" name="produkt[]">
                                        <option selected>Produkt wählen</option>
                                        @foreach (App\Product::get() as $produkt)
                                        <option value="{{$produkt->id}}">{!! $produkt->produkt !!}</option>
                                        @endforeach
                                    </select>
                                </td>
                            </tr>
                        </div>
                    </tbody>
                </table>
                <div class="col-12 mt-2">
                    <a class="addRow" name="addRow" id="addRow">+ weiteres Produkt</a>
                </div>
                <div class="col-12 mt-4">
                    <button type="submit" class="btn btn-primary">SPEICHERN</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $('.addRow').on('click', function() {
        addRow();
    });

    function addRow() {
        var tr = '<tr>'+
                    '<td>'+
                        '<input type="text" class="form-control" name="menge[]" id="menge" value="{{ old('menge') }}">'+
                    '</td>'+
                    '<td>'+
                        '<input type="text" class="form-control" name="einheit[]" id="einheit" value="{{ old('einheit') }}">'+
                    '</td>'+
                    '<td>'+
                        '<select id="produkt" class="form-control" name="produkt[]">'+
                            '<option selected>Produkt wählen</option>'+
                            '@foreach (App\Product::get() as $produkt)'+
                            '<option value="{{$produkt->id}}">{!! $produkt->produkt !!}</option>'+
                            '@endforeach'+
                        '</select>'+
                    '</td>'+
                '</tr>';

        $('tbody').append(tr);
    };

</script>
4

1 回答 1

0

试试这个:

$data=$request->all();
    $lastid=delivery_note::create($data)->id;
    $delivery_note->datum = $data['datum'];
    $delivery_note->customer_id = $data['customer'];

    if(count($data['produkt']) > 0)
    {

        foreach($requsest->get('produkt') as $item => $value){
            $data = new yourModelName();
            $data->delivery_notes_id=$lastid;
            $data->menge=$request->menge[$item];
            $data->einheit=$request->einheit[$item];
            $data->product_id=$request->produkt[$item]
           $data->save();
           
        }
    }
    return $this->index()->with([
        'meldung_success' => 'Lieferschein wurde erstellt!'
    ]);
}
于 2021-02-08T12:25:46.263 回答