I'm trying to insert multiple row data in a single click but i got an error called Array to String Conversion. and also when i remove Brackets[] from Model like this:
protected $table = 'biniyojan';
it throw an another error like this: (Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, string given) How can i solve this issue??
/////View Blade////////
<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 ($bini as $bi)
<option value="{{$bi->kriyakalap}}"> {{$bi->kriyakalap}}</option>
@endforeach
</select>
</div>
<div >
<select name="debit_credit[]" id="debit_credit" required>
<option selected="selected" disabled>डेबिट / क्रेडिट</option>
</select>
</div>
<div >
<select name="debit_credit_type[]" id="debit_credit_type" required>
<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') }}
</div>
@endif
</div>
<div form-row col-x1-3 id="showdata" >
<p ></p>
<p id="showdata" ></p>
</div>
</form>`enter code here`
Here is my Code
/////Controller////
public function bbcreate(Request $request)
{
$data = $request->all();
dd($data);
$biniyojan_details = new BiniyojanDetails();
$biniyojan_details->school = $data['school'];
$biniyojan_details->source = $data['source'];
$biniyojan_details->kriyakalap = $data['kriyakalap'];
$biniyojan_details->debit_credit = $data['debit_credit'];
$biniyojan_details->debit_credit_type = $data['debit_credit_type'];
$biniyojan_details->cash = $data['cash'];
$biniyojan_details->save();
if (is_countable($data['school']) && count($data['school']) > 0)
{
foreach ($data['school'] as $item => $value) {
$data2 = array(
'details_id' => $biniyojan_details->id,
'date' => $data['date'][$item],
'ab' => $data['ab'][$item],
'school' => $data['school'][$item],
'behora' => $data['behora'][$item],
);
Biniyojan::create($data2);
}
}
return redirect()->back()->with('status', 'Inserted');
}
/////View Page /////////
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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 ($bini as $bi)
<option value="{{$bi->kriyakalap}}"> {{$bi->kriyakalap}}</option>
@endforeach
</select>
@foreach ($bini as $bi)
@endforeach
</div>
<div >
<select name="debit_credit[]" id="debit_credit" required>
<option selected="selected" disabled>डेबिट / क्रेडिट</option>
</select>
</div>
<div >
<select name="debit_credit_type[]" id="debit_credit_type" required>
<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>
//////jQuery For Repeat form////////[enter image description here][1]
<!-- form repeat -->
<script type="text/javascript">
$(document).ready(function() {
var html = '<span><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 ($bini as $bi) <option value="{{$bi->kriyakalap}}"> {{$bi->kriyakalap}}</option> @endforeach </select> @foreach ($bini as $bi) @endforeach </div> <div > <select name="debit_credit[]" id="debit_credit" required> <option selected="selected" disabled>डेबिट / क्रेडिट</option> </select> </div> <div > <select name="debit_credit_type[]" id="debit_credit_type" required> <option selected="selected" disabled>डेबिट / क्रेडिट प्रकार</option> </select> </div> <div > <input type="text" placeholder="रकम" name="cash[]" id="price" required> </div> <div > <input type="button" name="remove" id="remove" value="-"> </div> </div> </div></span>';
var max = 5;
var x = 1;
$("#add").click(function() {
if (x <= max) {
$("#form-field").append(html);
x ;
}
})
$("#form-field").on('click', '#remove', function() {
$(this).closest('span').remove();
x--;
});
});
</script>
<!-- form repeat -->
[1]: https://i.stack.imgur.com/rzb3C.png
CodePudding user response:
please try this:-
public function bbcreate(Request $request){
$biniyojan_details = new BiniyojanDetails();
$biniyojan_details->school = $request->school;
$biniyojan_details->source = $request->source;
$biniyojan_details->kriyakalap = $request->kriyakalap;
$biniyojan_details->debit_credit = $request->debit_credit;
$biniyojan_details->debit_credit_type = $request->debit_credit_type;
$biniyojan_details->cash = $request->cash;
$biniyojan_details->save();
if(!empty($request->school)){
for($i=0; $i< count($request->school); $i ){
$biniyojan = new Biniyojan();
$biniyojan->details_id = $biniyojan_details->id;
$biniyojan->date = $request->date[$i];
$biniyojan->ab = $request->ab[$i];
$biniyojan->school = $request->school[$i];
$biniyojan->behora = $request->behora[$i];
$biniyojan->save();
}
}
return redirect()->back()->with('status', 'Inserted');
}
CodePudding user response:
As you said you are trying to insert multiple row data
.
Then You must convert array
into json
to store it into database.
Do like this - json_encode($request->school,true);