Support the ongoing development of Laravel.io →
Database Eloquent

Hello community,

I am Laravel newbie, so please, be patient with me.

My tables:

artists
- id
- name

songs
- id
- title

artistables
- id
- artist_id
- artistable_id
- artistable_type
- joinphrase
- order

My models are set up correctly, I guess, because I can save and retrieve data, so I won't post them (they are pretty basic anyway, just with morphToMany / morphedByMany).

My admin.song.edit view, I managed to build my create / edit form, so it benefits from Form Model Binding:

@forelse($song->artists as $key => $artist)
					<div class="ui-state-default" id="artist">
						<div class="form-group">
							<div class="col-sm-1 col-sm-offset-1">
								{!! Form::text('artists[' . $key . '][pivot][order]', null, ['class' => 'form-control', 'type' => 'number', 'readonly']) !!}
							</div>
							<div class="col-sm-2">
								{!! Form::text('artists[' . $key . '][pivot][joinphrase]', null, ['class' => 'form-control', 'type' => 'text']) !!}
							</div>
							<div class="col-sm-4">
								{!! Form::select('artists[' . $key . '][id]', [null => 'Select...'] + $artists->toArray(), null, ['class' => 'form-control']) !!}
							</div>
							<div class="col-sm-2">
								<a class="btn btn-default">
									<span class="glyphicon glyphicon-move" aria-hidden="true"></span>
								</a>
							</div>
						</div>
					</div>
					@empty
					<div class="form-group">
						<div class="col-sm-1 col-sm-offset-1">
							{!! Form::text('artists[0][pivot][order]', null, ['class' => 'form-control', 'type' => 'number', 'readonly']) !!}
						</div>

						<div class="col-sm-2">
							{!! Form::text('artists[0][pivot][joinphrase]', null, ['class' => 'form-control', 'type' => 'text']) !!}
						</div>

						<div class="col-sm-4">
							{!! Form::select('artists[0][id]', [null => 'Select...'] + $artists->toArray(), null, ['class' => 'form-control']) !!}
						</div>
					</div>
					@endforelse

^^^ This works perfectly and form is automagically populated with my database data, BUT... to make this work, inputs with data from pivot table must have model[pivot][column] name, while sync() method expects them in format (id => ['column' => 'value']).

Right now I do it like this in my SongController:

public function update(Request $request, $id)
    {
        $input = $request->all();

        // Song
    	$song = Song::findOrFail($id);
        $song->update($request->all());

        $artists = [];
		
        // Cycle through Artists and rebuild array for sync(), TODO: creation of non-existent Artists on the fly
        foreach($input['artists'] as $artist)
        {
            $artists[$artist['id']] = $artist['pivot'];
        }
		
        // Sync Artists
        $song->artists()->sync($artists);
        
        return redirect()->route('admin.song.edit', [$song]);
    }

It WORKS, but is this the best / only way? Or is there some better, cleaner and more "magical" approach? Something like passing array to $song->update(); which takes care of relationships:

Thanks for your replies. :)

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.