PaulThomasWeb
PaulThomasWeb

Reputation: 57

Try to update relationship in belongtomany and with pivot

Hello im trying to update my data in my DB, but im stuck in here where i have to update an relation but that relation also has an pivot table.

It should search through all the connected "Moderators", on update one which is not already connected should be, and if theres already an connection to the "Track", then there should come an errormessage but thats not the problme right now. it already gives me the opurtunity to attach other moderators, but not the query to get the errormessage.

The "Track" is connected over an pivot table with the "Speaker" and on the Pivot table i have an column with "is_moderator".

This is the update Function of my Track Controller:

    public function update(Request $request, $id)
    {
        $track = Track::all()->find($id);
        if ($request->file('track_data')) {
            $hashedName = Hash::make($request->file('track_data')->getClientOriginalName()) . "." . $request->file('track_data')->getClientOriginalExtension();
            $request->track_data->storeAs('public/tracks', $hashedName);
            $track->data = $hashedName;
        } elseif ($request->track_title) {
            $track->title = $request->track_title;
        } elseif ($request->track_description) {
            $track->description = $request->track_description;
        } elseif ($request->input('topics')) {
            foreach($request->input('topics') as $topicIds){
                $topicId = intval($topicIds);
                $topicTrackId = $track->topics()->get();
                for ($i = 0; $i < count($topicTrackId); $i++) {
                    if ($topicId == $topicTrackId[$i]->id) {
                        return redirect()->route('admin.trackEdit', $track->id)->withErrors(['msg' => 'Topic wurde bereits ausgewählt']);
                    }
                }
            }
            $track->topics()->attach($request->input('topics'));
        } elseif ($request->input('moderators')) {
            $data = [];
            foreach ($request->input('moderators') as $moderatorId) {
                $data[$moderatorId] = ['is_moderator' => 1];
            }

            $track->speakers()->attach($data);
        }
        $track->update();
        return redirect()->route('admin.trackShow');
    }

This is my EditBlade right now:

@extends('layouts.app')

@section('content')
    <form 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">
        <label for="track_description">Track-Description</label>
        <input type="text" name="track_description" id="track_description">
        <label for="track_data">Track-Data</label>
        <input type="file" name="track_data" id="track_data">
        <select name="season_id" id="season_id">
            <option selected value="">Keine Auswahl</option>
            @foreach ($seasons as $season)
                <option value="{{ $season->id }}">{{ $season->name }}</option>
            @endforeach
        </select>
        <div class="container mt-5">
            <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($speakers as $speaker)
                    <option value="{{ $speaker->id }}">{{$speaker->getFullName()}}</option>
                @endforeach
            </select>
        </div>
        <div class="container mt-5">
            <label>Speakers</label>
            <select class="form-select" multiple="multiple" name="speakers[]">
                @foreach($speakers as $speaker)
                    <option value="{{$speaker->id}}">{{$speaker->getFullName()}}</option>
                @endforeach
            </select>
        </div>
        <button type="submit">submit</button>
    </form>
@endsection

The Model of the Track:


    class Track extends Model
{
    use HasFactory;

    protected $table = 'tracks';

    protected $primaryKey = 'id';

    protected $fillable = [
            'title',
            'description',
            'data',
            'season_id',
        ];

    public function season(): BelongsTo
    {
        return $this->belongsTo(Season::class);
    }

    public function speakers(): BelongsToMany
    {
        return $this->belongsToMany(Speaker::class, 'speakers_tracks', 'track_id', 'speaker_id')->withPivot('is_moderator');
    }

    public function topics(): BelongsToMany
    {
        return $this->belongsToMany(Topic::class, 'topics_tracks', 'track_id', 'topic_id');
    }

}

And the Speaker Model:


    class Track extends Model
{
    use HasFactory;

    protected $table = 'tracks';

    protected $primaryKey = 'id';

    protected $fillable = [
            'title',
            'description',
            'data',
            'season_id',
        ];

    public function season(): BelongsTo
    {
        return $this->belongsTo(Season::class);
    }

    public function speakers(): BelongsToMany
    {
        return $this->belongsToMany(Speaker::class, 'speakers_tracks', 'track_id', 'speaker_id')->withPivot('is_moderator');
    }

    public function topics(): BelongsToMany
    {
        return $this->belongsToMany(Topic::class, 'topics_tracks', 'track_id', 'topic_id');
    }

}

If you can help me i will be very glad it tooked me already two hours of headhurtings. Thank you in advance!

Upvotes: 0

Views: 43

Answers (1)

ali heydari
ali heydari

Reputation: 86

You have to change attaching speakers, from:

 foreach ($request->input('moderators') as $moderatorId) {
                $data[$moderatorId] = ['is_moderator' => 1];
            }
            $track->speakers()->attach($data);

to:

   foreach ($request->input('moderators') as $moderatorId) {
            $track->speakers()->attach($moderatorId, ['is_moderator' => 1] );
            }

Upvotes: 1

Related Questions