im trying to build an multiselect with laravel in an update form, it should firstly select all what is related to the given id. then should show all the other entries which are not related already.
This is the whole function for the updateControl
public function update(Request $request, Track $track): RedirectResponse
{
//check if data input if yes delete old file and save new one
if ($request->file('track_data')) {
File::delete(public_path('/uploads/tracks/'. $track->getAttribute('data')));
do {
$hashedName = Helper::getRandomString().".".$request->file('track_data')->getClientOriginalExtension();
} while(Track::where(Track::TRACK_DATA, $hashedName)->count());
$request->file('track_data')->storeAs('/uploads/tracks', $hashedName);
$track->setAttribute(Track::TRACK_DATA, $hashedName);
}
//TrackTitle
$track->setAttribute(Track::TRACK_TITLE, $request->get('track_title'));
//TrackDesc
$track->setAttribute(Track::TRACK_DESCRIPTION, $request->get('track_description'));
//checks if topics have input if yes check again if they are already connected if yes throw error if no save the new one
if ($request->input('topics')) {
$track->topics()->detach();
$track->topics()->attach($request->input('topics'));
}
if ($request->input('speakers')){
$track->speakers()->detach();
$track->speakers()->attach('speakers');
}
//checks if moderators have input, if yes check again if they are already connected, if yes throw error - if no save the new one
if ($request->input('moderators')) {
$data = [];
foreach ($request->input('moderators') as $moderatorId) {
$data[$moderatorId] = ['is_moderator' => 1];
};
$track->speakers()->attach($data);
}
//and check write them as moderator in table
$track->update();
return redirect()->route('admin.trackShow');
}
}
This is the Form in my Blade
<form class="d-flex flex-column" action="{{ route('admin.trackUpdate', $track->id) }}" method="post" enctype="multipart/form-data" id="track-form">
@csrf
{{ method_field('patch') }}
@include('errors.alert')
<label for="track_title">Track-Name</label>
<input type="text" name="track_title" id="track_title" value="{{$track->title}}">
<label for="track_description">Track-Description</label>
<input type="text" name="track_description" id="track_description" value="{{ $track->description }}">
<label for="track_data">Track-Data</label>
<input type="file" name="track_data" id="track_data" value="">
<label for="season_id">Seasonauswahl</label>
<select name="season_id" id="season_id">
<option value="{{$track->season()->get()[0]->id}}">Aktuell: {{$track->season()->get()[0]->name}}</option>
@foreach ($seasons as $season)
<option value="{{ $season->id }}">{{ $season->name }}</option>
@endforeach
</select>
<div class="container mt-2">
<label>Topics</label>
<select class="form-select" multiple="multiple" name="topics[]">
@foreach($topics as $topic)
<option value="{{$topic->id}}">{{$topic->name}}</option>
@endforeach
</select>
</div>
<div class="container mt-5">
<label>Moderator</label>
<select class="form-select" multiple="multiple" name="moderators[]">
@foreach($track->speakers()->get() as $moderatorTrack)
@if($moderatorTrack->pivot->is_moderator)
<option selected value="{{$moderatorTrack->id}}">{{$moderatorTrack->getFullName()}}</option>
@else
<option value="{{$moderatorTrack->id}}">{{$moderatorTrack->getFullName()}}</option>
@endif
@endforeach
</select>
</div>
<div class="container mt-5">
<label>Speakers</label>
<select class="form-select" multiple="multiple" name="speakers[]">
@foreach($track->speakers()->get() as $speakerTrack)
@if($speakerTrack->pivot->is_moderator == null)
<option selected value="{{$speakerTrack->id}}">{{$speakerTrack->getFullName()}}</option>
@else
<option value="{{$speakerTrack->id}}">{{$speakerTrack->getFullName()}}</option>
@endif
@endforeach
@foreach($speakers as $speaker)
<option value="{{$speaker->id}}">{{$speaker->getFullName()}}</option>
@endforeach
</select>
</div>
<button type="submit">submit</button>
</form>
</div>
</div>
My Models
TopicModel
class Topic extends Model
{
use HasFactory;
public const TOPIC_NAME = 'name';
protected $fillable = [
self::TOPIC_NAME,
];
/**
* @return BelongsToMany
*/
public function tracks():BelongsToMany
{
return $this->belongsToMany(Track::class, 'topics_tracks', 'topics_id', 'track_id');
}
}
SpeakerModel
class Speaker extends Model
{
use HasFactory;
public const SPEAKER_FIRST_NAME = 'first_name';
public const SPEAKER_LAST_NAME = 'last_name';
public const SPEAKER_IS_MODERATOR = 'is_moderator';
protected $fillable = [
self::SPEAKER_FIRST_NAME,
self::SPEAKER_LAST_NAME,
self::SPEAKER_IS_MODERATOR,
];
/**
* @return BelongsToMany
*/
public function tracks():BelongsToMany
{
return $this->belongsToMany(Track::class, 'speakers_tracks', 'speaker_id', 'track_id')->withPivot('is_moderator');
}
/**
* @return string
*/
public function getFullName() : string
{
return $this->getAttribute(self::SPEAKER_FIRST_NAME) . " " . $this->getAttribute(self::SPEAKER_LAST_NAME);
}
}
TrackModel
class Track extends Model
{
use HasFactory;
public const TRACK_TITLE = 'title';
public const TRACK_DESCRIPTION = 'description';
public const TRACK_SEASON_ID = 'season_id';
public const TRACK_DATA = 'data';
protected $fillable = [
self::TRACK_TITLE,
self::TRACK_DATA,
self::TRACK_DESCRIPTION,
self::TRACK_SEASON_ID,
];
/**
* @return BelongsTo
*/
public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
/**
* @return BelongsToMany
*/
public function speakers(): BelongsToMany
{
return $this->belongsToMany(Speaker::class, 'speakers_tracks', 'track_id', 'speaker_id')->withPivot('is_moderator');
}
/**
* @return BelongsToMany
*/
public function topics(): BelongsToMany
{
return $this->belongsToMany(Topic::class, 'topics_tracks', 'track_id', 'topic_id');
}
Thanks for any Help i dont know how to move on!