htmlspecialchars() expects parameter 1 to be string, array given

Posted 1 year ago by nanadjei2

I am trying to post an array of data. But I keep getting this error htmlspecialchars() expects parameter 1 to be string, array given

This is my view:

    <div class="form-group row">
    <div class="col-md-10">
        <input type="text" class="form-control" name="name[]" id="testOptions" placeholder="Enter option two"
        value="{{ old('name.1') }}">
    </div>
    <div class="col-md-2">
        <input type="text" class="form-control" name="score[]" id="testOptions" placeholder="Enter option two score"
        value="{{ old('score.1')}}">
    </div>
</div>

<div class="form-group row">
    <div class="col-md-10">
        <input type="text" class="form-control" name="name[]" id="testOptions" placeholder="Enter option two"
        value="{{ old('name.2') }}">
    </div>
    <div class="col-md-2">
        <input type="text" class="form-control" name="score[]" id="testOptions" placeholder="Enter option two score"
        value="{{ old('score.2') }}">
    </div>
</div>

<div class="form-group row">
    <div class="col-md-10">
        <input type="text" class="form-control" name="name[]" id="testOptions" placeholder="Enter option two"
        value="{{ old('name.3') }}">
    </div>
    <div class="col-md-2">
        <input type="text" class="form-control" name="score[]" id="testOptions" placeholder="Enter option two score"
        value="{{ old('score.3') }}">
    </div>
    </div>

If I dd($request->all(). I get...

  "name" => array:4 [▼
    0 => "Keelie Bennett"
    1 => "Solomon Pierce"
    2 => "Jessica Crosby"
    3 => "Kaden Thomas"
  ]

  "score" => array:4 [▼
   0 => "12"
   1 => "21"
   2 => "31"
   3 => "21"
  ]

In my Test Class:

 public function options()
    {
      return $this->hasMany(TestOption::class, 'test_id');
    }

Controller:

    public function store(Request $request)
       {

       $request->validate(Test::$rules);

       $request->validate(\App\TestOption::$rules);

       return 'Passed validations';

       $test = Test::create(['title' =>  $request->title, 'question' =>  $request->question]);

       //$test->options->sync($request->names);

       $options = [];

       foreach ($request->name as $option) {

        $options[$option] = ['score' => $request->score];

       }

       $test->options->sync($options);

      return back()->with('success', 'You have successfully added a new test');

 }

Kindly help me. Thank you

Please sign in or create an account to participate in this conversation.