Recovers old values and data stored in database checkbox multi array
hello, (again) before i begin srry for my bad english.
it's school project : An user can post a demande, with a form :
the form recovers info of the demande(title, body...) and infos of the student(name, regions, skills...). when sending a form it's goes in two table : Demande and Student.
Demande and Student = has many Skills = many to many
the 3 Tables are in a classic relation ship
class Demande extends Model
{
...
public function student()
{
return $this->belongsTo(Student::class);
}
public function skills()
{
return $this->belongsToMany(Skills::class)->withTimestamps();
}
...
}
class Student extends Model
{
public function demandes()
{
return $this->hasMany(Demande::class);
}
public function skilles()
{
return $this->belongsToMany(Skills::class)->withTimestamps();
}
}
class Skills extends Model
{
public function demandes()
{
return $this->belongsToMany(Demande::class)->withTimestamps();
}
}
I would like to retrieve old values in my form and at the same time recovers in edit page recover the data stored in the database.
here is my code:
<div class="row checkbox-offres">
<legend class="fieldset-label">Skills :</legend>
@foreach ($skills as $skill)
<div class="form-group-lg col-lg-6">
<label>
<input class="form-check-input @error('skills') is-invalid @enderror" type="checkbox"
name="skills[]" value='{{$skill->id}}' @foreach($demande->skills as $skill_chosen)
{{ (is_array(old('skills')) && in_array($skill->id, old('skills'))) ? 'checked' : '' }}
@endforeach
> {{$skill->name}}</label>
</div>
@endforeach
@error('skills')
<div class="invalid-feedback d-block">
{{$errors->first('skills')}}
</div>
@enderror
</div>
this code doesn't recovers old values in the add form and doesen't recovers data stored in databse. but recover old in the edit id someone mistake a field it's weird.
normally if I do
{{ $skill_chosen->id == $skill->id ? 'checked' : '' }}
it's retrieve data form database but i want the old to when a user miss the field.
Please or to participate in this conversation.