How not to display the language if the user has this language?
I display all the languages
in a view by using foreach
but I don't want to display the language if the user has that language saved .
As an example
As options in the select, I have 3 languages (english, Korean, Japanese).
If the user has saved English as language in the table, it should'nt be displayed in the options.
This is the model for language
.
protected $fillable = [
'language'
];
public function userlanguage()
{
return $this->hasMany(UserLanguage::class);
}
this is my model for the user
protected $fillable = [
'user',
'language_ID',
];
public function languages()
{
return $this->belongsTo(Languages::class, 'language_ID');
}
This is the controller
public function index()
{
$userlanguages = UserLanguage::with('user', 'languages', 'proficiency')->where('user_ID', Auth::user()->id)->get();
$languages = Languages::all();
return view('userlanguage',[
'languages' => $languages,
'userlanguages' => $userlanguages
]);
}
This is the view
<select name="language">
@foreach( $languages as $language )
<option value="{{ $language->id }}">{{ $language->language }}</option>
@endforeach
</select>
</select>
I don't know how to not display the user's saved language in the options.
If you want to hide visually, you can add a class to option like
<option
:class="{hidden: $language->id == $userLanguage->id}"
value="{{ $language->id }}"
>{{ $language->language }}</option>
//in CSS
.hidden {display: none;}
If you want to exclude the language completely:
<select name="language">
@foreach( $languages->filter(fn ($l) => $l->id != $userLanguage->id) as $language )
<option value="{{ $language->id }}">{{ $language->language }}</option>
@endforeach
</select>