Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

data failed to save into the database

I have these models:

class Rating extends Model
 {
    public $timestamps = false;

    protected $table = 'ratings';

    protected $primaryKey = 'id';

    protected $fillable = [
                  'rating_description',
                  'rating_value',
              ];

 }

class EmployeeRating extends Model
{
   public $timestamps = false;
   protected $table = 'employee_ratings';

   protected $primaryKey = 'id';

   protected $fillable = [
                'employee_id',  
                'skill_id',
                'rating_id',
                'comment',
            ];
  
  
   protected $casts = [
    'data' => 'array',
   ];

  public function employees()
  {
      return $this->belongsTo('App\Models\Employee');
  } 
  
  public function skills()
  {
      return $this->belongsTo('App\Models\Skill', 'skill_id', 'id' );
  }     

  public function ratings()
  {
      return $this->belongsTo('App\Models\Rating', 'rating_id', 'id' );
  } 

}

I iterate using skill

Controller

public function creat($id)
{
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;
    $employee = Employee::where('id', $id)->first();
    $skills = Skill::where('company_id', $userCompany)->get();
    $ratings = Rating::where('company_id', $userCompany)->get(); 
    $count_ratings = Rating::where('company_id', $respondent->company_id)->get()->count(); 
    return view('employee_ratings.create')
            ->with('skills', $skills)
            ->with('ratings', $ratings)
            ->with('count_ratings', $count_ratings)
            ->with('employee', $employee);
}  	

public function store(StoreEmployeeRequest $request,$id)
{
        foreach ($request->skill_id as $key => $skill_id){

    $insert_array = [
            'skill_id'                        => $request->appraisal_skill_id[$key],
            'rating_id'                    => $request->rating_id[$key],
            'comment'                                   => $request->comment[$key],
        ];
     EmployeeRating::create($insert_array );
 }

The form looks like this:

https://i.stack.imgur.com/t3I9h.png

view

   <table id="msfTable" class=" table table-bordered table-striped table-hover datatable">
        <thead>
           <tr>
              <th scope="col" width="4%">ID</th>
              <th scope="col" width="25%">Core Value<span style="color:red;">*</span></th>                                    
              <th scope="col" class="text-center" width="25%" colspan="{{$count_ratings}}">Rating<span style="color:red;">*</span></th>                                                                    
              <th scope="col" width="46%">Comment</th>
                  </tr>
               </thead> 
               <thead>
                  <tr>
                    <th scope="col" width="4%"></th>
                    <th scope="col" width="25%"></th>                                    
                       @foreach($ratings as $rating)
                    <th scope="col" width="25%">
                        {{$rating->rating_description}}
                      </th>
                     @endforeach                                                                                                         
                        <th scope="col" width="46%"></th>
                  </tr>
              </thead>                             
              <tbody>
                @foreach ($skills as $key => $skill)
                  <tr>
                    <td width="4%">
                       {{$key+1}}
                   </td>
                  <td width="25%">
                   <span>{{$skill->skill_name}}</span>                                     
                </td>                                    
                                @foreach($ratings as $index => $rating)
                    <td><input type="radio" name="skill[{{ $key}}]rating" id="{!! $rating->id !!}" @if (!$index) {!! "checked" !!} @endif required></td>
                                @endforeach                                                                       
                    <td width="46%">    
                       <input type="text" name="comment[]" placeholder="Enter comment here" class="form-control comment">
                     </td>                                      
                </tr>
            @endforeach
      </tbody>
  </table>

When I submitted, I got this error:

 skill_id field is required
 rating_id field is required

Yet the fields are not empy.

How do I resolve this?

Thanks

0 likes
27 replies
noblemfd's avatar

@michaloravec - I have done this:

Controller

public function store(StoreEmployeeRequest $request,$id)
{
        foreach ($request->skill_id as $key => $skill_id){

    $insert_array = [
            'skill_id'                        => $request->appraisal_skill_id[$key],
            'rating_id'                    => $request->rating_id[$key],
            'comment'                                   => $request->comment[$key],
      ];
     EmployeeRating::create($insert_array );
 }

View

                        <tbody>
                            @foreach ($skills as $key => $skill)
                            <tr>
                                <td width="4%">
                                    {{ $loop->iteration }}
                                </td>
                                <td width="25%">
                                    <span>{{$skill->skill_name}}</span> 
                                    <input type="hidden" name="appraisal_skill_id[]" value="{{$skill->id}}">                                    
                                </td>                                    
                                @foreach($ratings as $index => $rating)
                                  <td><input type="radio" name="skill[{{$key}}]rating" id="{!! $rating->id !!}" @if (!$index) {!! "unchecked" !!} @endif required></td>
                                @endforeach                                                                       
                                <td width="41%">    
                                    <input type="text" name="comment[]" placeholder="Enter comment here" class="form-control comment">
                                </td>                                      
                            </tr>
                            @endforeach
                        </tbody>

The only error I now have is:

rating_id field is required

How do I transfer this skill[{{ $key}}]rating from view blade to the controller as in 'rating_id' => $request->rating_id[$key] to insert in database

automica's avatar

@noblemfd in your form your rating is being set on

skill[{{$key}}]rating

so in your $request you should be getting it from:

$request->skill[$key]['rating']

to debug what you are getting from your request in your controller.

dd($request->all())
noblemfd's avatar

@automica - I added

public function store(StoreEmployeeRequest $request,$id)
{
        dd($request->all());

        foreach ($request->skill_id as $key => $skill_id){

    $insert_array = [
            'skill_id'                        => $request->appraisal_skill_id[$key],
            'rating_id'                    => $request->skill[$key]['rating'],
            'comment'                                   => $request->comment[$key],
      ];
     EmployeeRating::create($insert_array );
 }

It escaped dd($request->all()) and still gave me the same error

Snapey's avatar

ALWAYS CHECK AND UNDERSTAND THE DATA YOU GET FROM FORMS !!!!!!

Snapey's avatar

The only error I have now is rating_id field is required

Where does that error appear

What file

What line

What is the exact wording

automica's avatar

@noblemfd so error Is in StoreEmployeeRequest and what you've got set up for your validation rules.

Please post that file.

noblemfd's avatar

@snapey - I set validation in RequestRules:

class StoreEmployeeRequest extends FormRequest
{
  public function authorize()
  {
      return \Gate::allows('employee_rating_create');
  }

  public function rules()
  {
    return [   
                
        'skill_id'           => 'required|array',
        'skill_id.*' => [
             'required',         

        'rating_id'           => 'required|array', 
        'rating_id.*' => [
             'required',                   
        ],                     
       
        'comment'           => 'nullable|array',
        'comment.*' => [
             'nullable', 
         
        ],         
    ];
  }               
}

So it's picking the error message from :

        'rating_id'           => 'required|array', 
        'rating_id.*' => [
             'required',                   
        ],
automica's avatar
automica
Best Answer
Level 54

@noblemfd you aren't passing an array of rating_id into your request.

$request->skill[$key]['rating']

so validation should be

 'skill.*.rating' => [ 'required' ]

and you don't need:

  'rating_id'           => 'required|array',

otherwise change input to

"rating_id[{{ $key}}]"

request

 $request->skill[$key]['rating']

to

 $request->rating_id[$key]['rating']

and keep your validation the same.

MichalOravec's avatar

He is same as oxbir. Instead of reading documentation and watching videos on Laracasts, he asks stupid questions every day.

Statistics speak for themselves

  • Total experience - 28,270
  • Lesson completed - 0
  • Best reply awards - 0
noblemfd's avatar

@automica - I used

"rating_id[{{ $key}}]" in view

$request->rating_id[$key] in controller

dd($request->all()) gives me:

 "skill_id" => array:8 [▼
   0 => "1"
   1 => "2"
   2 => "3"
   3 => "4"
   4 => "5"
   5 => "6"
   6 => "7"
   7 => "8"
 ]
 "rating_id" => array:8 [▼
   0 => "on"
   1 => "on"
   2 => "on"
   3 => "on"
   4 => "on"
   5 => "on"
   6 => "on"
   7 => "on"
 ]
"comment" => array:8 [▼
   0 => "dsddsdsds"
   1 => "ddsdsdsds"
   2 => "uuuuuuuuuuuuuuuuuyy"
   3 => "hhhggh"
   4 => null
   5 => null
   6 => null
   7 => null
 ]

Why is rating_id giving "on", I expect it to display the id as in skill_id

automica's avatar

@michaloravec I can half get not wanting to pay for a subscription but the time extra it takes to develop (which someone must be paying for) vs $8 pcm (on annual sub) makes no sense. Like $2 per week is too much?

Atleast oxbir has done 3 lessons now....

automica's avatar

@noblemfd you need another key to show which of you radios has been selected:

"rating_id[{{$key}}][{{$index}}]"
MichalOravec's avatar

@automica Exactly it's almost nothing to compare how much time they waste on this forum by asking really silly questions.

And when I see something like "Answer my question, I am in hurry" by @oxbir

or asking same questions about dates by @noblemfd

It's really ridiculous.

I'm done with these guys.

They have to study by reading documentation and watching videos and trying to solve their problem by yourself.

That's it.

Nobody should help them anymore.

1 like
noblemfd's avatar

@automica - Problem solved. Just added value="{{$rating->id}}" in the view blade. Thanks

Snapey's avatar

@automica The radios should be grouped with the value showing which rating was selected.

ie, all the radios within the same row should have the same name

I keep trying to educate @noblemfd how to name things so that he(?) does not keep end up having to iterate through parallel arrays, but always reverts to the dumb way to name things.

automica's avatar

@snapey ah, so that's why the 'rating' suffix appeared?

eg

"rating_id[{{$key}}['rating']"

anyway appears be solved now anyways.

Snapey's avatar

@automica

in a previous question, i suggested name='skill[{{ $key }}]rating'. so that he gets an array of skill attributes, one of which is rating

it follows then to also using the same naming scheme for name='skill[{{ $key }}]comment'

Then there is an array of skills, each with single rating and comment value instead of independent arrays

automica's avatar

@snapey so that would have worked if he'd updated his validation, which I also suggested.

noblemfd's avatar

@snapey - Initially, I used this name='skill[{{ $key }}]rating in the blade, but kept on having validation issue.

But, what could I have done?

automica's avatar

@noblemfd you could have changed the validation to match the field you are trying to validate.

Snapey's avatar
'skill.*.rating' => 'required'

for instance

Please or to participate in this conversation.