Why you don't follow advices when somebody gave it to you?
Oct 12, 2020
27
Level 9
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
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.
Please or to participate in this conversation.