1

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...

1
  • Have you tried running a foreach loop and trigger the save option inside the loop ? Commented Jul 28, 2021 at 7:53

1 Answer 1

1

Try this :

   $test_result=$request->test_result;

    foreach($request->test_type as $item => $value)
    $data[$value]=array(
        'test_id'=>$request->test_id,
        'test_type'=>$value,
        'user_id'=>$test_result[$item],
        'test_by'=>$request->test_by,
    );
    
    TestResult::insert($data); // Eloquent approach

i hope you got your solution .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.