$channel_mapping->channel()->create(['name' => $name]);
https://laravel.com/docs/6.x/eloquent-relationships#the-create-method
I have a channel mapping model with;
public function channel(): BelongsTo
{
return $this->belongsTo(Channel::class);
}
After I have created a channel, I need to link it to the channel mapping. With belongsToMany we can do attach, but with just belongsTo do we have a better option than this?
$channel = Channel::firstOrCreate(['name' => $name]);
$channel_mapping->update(['channel_id' => $channel->id]);
I was thinking something like this should exist?
$channel = Channel::firstOrCreate(['name' => $name.'/'.$identifier]);
$channel_mapping->attach($channel);
This is actually what I was looking for. It does them same thing under the hood I am sure but communicates much better.
$channel = Channel::firstOrCreate(['name' => $name]);
$channel_mapping->channel()->associate($channel)->save();
Please or to participate in this conversation.