in my laravel application, I have the following form to submit datas
{!! Form::open(array('route' => 'testresults.store','method'=>'POST')) !!}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Test ID:</strong>
@foreach ($getTests as $object)
{!! Form::text('test_id', ''.$object->id .'', array('placeholder' => 'Test Name','class' => 'form-control','readonly')) !!}
@endforeach
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<table class="table table-bordered">
<tr>
<th>Test</th>
<th width="280px">Result</th>
</tr>
@foreach ($getTests as $key => $getTest)
@foreach (explode(',', $getTest->samp_list) as $samp_list)
<tr>
<td>
{!! Form::text('test_type[]', ''.$samp_list.'', array('placeholder' => 'Test Type','class' => 'form-control','readonly')) !!}
</td>
<td>{!! Form::text('test_result[]', null, array('placeholder' => 'Test Result','class' => 'form-control')) !!}</td>
</tr>
@endforeach
@endforeach
</table>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Test By:</strong>
{!! Form::text('test_by', ''.Auth::user()->name .'', array('placeholder' => 'Test Name','class' => 'form-control','readonly')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
This is my create.blade.php
And my controller looks like this
public function store(Request $request)
{
try{
request()->validate([
'test_id' => 'required',
'test_type' => 'required',
'test_result' => 'required',
'test_by' => 'required',
]);
TestResult::create($request->all());
return redirect()->route('testresults.index')
->with('success','Test Type created successfully.');
}
catch(Exception $e){
return redirect()->route('testresults.create')
->with('failed','An error has been occured.');
}
}
Now the problem is, whenever I tried to store the data, they save it as arrays in the database.
Eg:test_type will save as something like ["TS","Cu ion content"] and test_result will save as ["3","45%"] in the same row.
But I want those data to be saved in separate rows in string format.
Eg:
row1-> id |1 and test_type | TS and test_result | 3
row2-> id |2 and test_type | Cu ion content and test_result | 45%
.....
rown-> id |n and test_type | N and test_result | n
In my model, I have this,
protected $casts = [
'test_type' => 'array',
'test_result' => 'array',
];
Once I remove it, I'm getting an error saying
ErrorException Array to string conversion
Now the issue is, all the records are save as arrays in the same row...