I guess first I would go over what I know:
I know for sure an artist can have many albums
Additionally I know that an album can have many artists
My rough table structure (simplified for ease of description) would be like this:
Schema::create('albums', function ($table) {
$table->increment('id');
$table->string('name');
});
Schema::create('artists', function ($table) {
$table->increment('id');
$table->string('name');
});
Schema::create('album_artist', function ($table) {
$table->unsignedInteger('album_id');
$table->unsignedInteger('artist_id');
$table->primary(['album_id','artist_id']);
$table->foreign( 'album_id' )
->references( 'id' )
->on( 'albums' )
->onDelete( 'cascade' );
$table->foreign( 'artist_id' )
->references( 'id' )
->on( 'albums' )
->onDelete( 'cascade' );
});
Then in my artists model
public function albums(){
return $this->belongsToMany(Album::class,'album_artists');
}
Then my album model
public function getHasMultipleArtistsAttribute(){
return $this->artists->count > 1;
}
public function getArtistAttribute(){
if( $this->artists->count > 1 ){
return false;
}else{
return $this->artists()->first();
}
}
public function artists(){
return $this->belongsToMany(Artist::class);
}
I would then use some basic logic to decide if this belongs to a single or multiple artists when it comes to an album, which you can do by checking the length (count) of the artists relationship.
I feel that going about it considering only 1 artist per album would be more work than it would be worth, as you already know you want the ability to have multiple artists.
You could also have an option attribute on the album (i have the demonstrated in the model for album.. but this is of course optional
Nothing is highly optimized, but this is the general concept I would use for it... modifying the code as required.