Home > Software design >  Wanted to store multiple rows but only one row inserted
Wanted to store multiple rows but only one row inserted

Time:09-17

Here is the View Page Form and the controller. I wanted to insert multiple rows at time but this code insert only one row. I have tried so many methods but none of them are useful. What are the problem of this code and How can i solve this issue? What are the problem of this code and How can i solve this issue?

///view Form////

<form method="POST" action="{{ route('bbcreate') }}">
    @csrf
    <div >
        <div >
            <input type="date" placeholder="मिति" value="@php echo $today; @endphp" name="date"  id="inputCity" required>
        </div>
        <div >
            <select id="inputState" placeholder="" name="ab"  required>
                <option>2079-080</option>
                <option>2078-079</option>
            </select>
        </div>



        <div >
            <select id="inputState" name="school"  required>
                <option selected disabled>स्रोत पाउने संस्था </option>
                @foreach ($school_array as $sch)
                <option value="{{ $sch -> name }}">{{ $sch -> name}}</option>
                @endforeach
            </select>
        </div>
    </div>
    <div id="form-field">
        <div >
            <div >
                <select id="source" name="source[]"  required>
                    <option selected disabled value="">स्रोत</option>
                    <option>केन्द्र</option>
                    <option>प्रदेश</option>
                    <option>स्थानीय</option>
                    <option>अन्य</option>
                </select>
            </div>
            <div >
                <select id="kriyakalap" name="kriyakalap[]"  required>
                    <option selected disabled>क्रियाकलाप</option>
                    @foreach ($kriyakalap_array as $bi)
                    <option value="{{$bi->kriyakalap}}"> {{$bi->kriyakalap}}</option>
                    @endforeach

                </select>

            </div>
            <div >
                <select name="debit_credit[]" id="debit_credit"  >
                    <option selected="selected" disabled>डेबिट / क्रेडिट</option>
                </select>
            </div>
            <div >
                <select name="debit_credit_type[]" id="debit_credit_type"  >
                    <option selected="selected" disabled>डेबिट / क्रेडिट प्रकार</option>
                </select>
            </div>

            <div >
                <input type="text" placeholder="रकम" name="cash[]"  id="price" required>
            </div>
            <div >
                <input type="button"  name="add" id="add" value=" ">
            </div>
        </div>
    </div>
    <div  style="justify-content:left ;">
        <div >
            <input type="text" placeholder="ब्यहोरा" name="behora"  id="behora" required>
        </div>

        <div >
            <input type="submit" id="submit" name="submit" value="राख्नुहोस्" >

        </div>

        @if (session('status'))
        <div >
            {{ session('status') }}
            {{-- message --}}
        {!! Toastr::message() !!}
        </div>
        @endif

    </div>
    <div form-row col-x1-3 id="showdata" >
        <p ></p>
        <p id="showdata" ></p>
    </div>
</form>

///controller////

public function bbcreate(Request $request)
{
        $biniyojan = new Biniyojan();
        //$biniyojan->details_id = $biniyojan_details->details_id;
        $biniyojan->date =  $request->date;
        $biniyojan->ab =  $request->ab;
        $biniyojan->school =  $request->school;
        $biniyojan->behora =  $request->behora;
        $biniyojan->save();

    if (!empty($request->school)) {
        for ($i = 0; $i < count((array)$request->school); $i  ) {
        $biniyojan_details = new BiniyojanDetails();
         
        $biniyojan_details['biniyojan_id'] = $biniyojan->id;
        $biniyojan_details->school = $request->school;
        $biniyojan_details->source = $request->source[$i];
        $biniyojan_details->kriyakalap = $request->kriyakalap[$i];
        //$biniyojan_details->debit_credit = $request->debit_credit[$i];
        //$biniyojan_details->debit_credit_type = $request->debit_credit_type[$i];
        $biniyojan_details->cash = $request->cash[$i];
        $biniyojan_details->save();
        }
    }

 return redirect()->back()->with('status', 'Inserted');
}

CodePudding user response:

This is how you should do it. You should create an object on every instance of the loop.

public function bbcreate(Request $request)
{
    //$biniyojanDetailsToBeInserted = [];

    if (!empty($request->school)) {
        for ($i = 0; $i < count((array)$request->school); $i  ) {
        $biniyojan_details = new BiniyojanDetails(); // the object
        
        //$biniyojan_details['biniyojan_id'] = $biniyojan->id;
        $biniyojan_details->school = $request->school;
        $biniyojan_details->source = $request->source[$i];
        $biniyojan_details->kriyakalap = $request->kriyakalap[$i];
        //$biniyojan_details->debit_credit = $request->debit_credit[$i];
        //$biniyojan_details->debit_credit_type = $request->debit_credit_type[$i];
        $biniyojan_details->cash = $request->cash[$i];
        $biniyojan_details->save();
        }
    }

    /* if ($biniyojanDetailsToBeInserted) {
        BiniyojanDetails::insert($biniyojanDetailsToBeInserted);
    } */

     return redirect()->back()->with('status', 'Inserted');
}

  • Related