'Selected on editing the data with pivot table values

I have 3 tables,

$student= Student::with('zones', 'appliances','phones')->findOrFail($id);
$appliances = Appliance::all('id', 'name');

I want to get the appliances field selected on editing student.

public function phones()
{
    return $this->hasMany(Phone::class);
}

public function appliances()
{
    return $this->belongsToMany(Appliance::class, 'dealer_appliances');
}

I get collection when I dd(user.appliances). How can I get these fields selected in select field on edit?



Solution 1:[1]

I do not know If It is a good approach, but I solved this way:

{{ in_array($appliance->id, $data->appliances->pluck('id')->toArray()) ? 'selected' : ''}} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcel Gruber