ddoddsr's avatar

Camper Session pivot table

I want to add conversations to a pivot table

{
    protected $table = 'camper_session';

    public function conversations()
    {
        return $this->hasMany(Conversation::class);
    }
}

Inverse relationship

{
    public function camperSession()
    {
        return $this->belongsTo(CamperSession::class);
    }
}

how do I reference the pivot table to ->associate the conversation to the pivot table?

0 likes
4 replies
ddoddsr's avatar

Is there a best way to get the camper_session id than a

$id = CamperSession::where('camper',12)-where('session',4)'

jjoek's avatar

depending on where you are coming from either from the Camper or the Session you most likely would have loaded your pivot using withPivot and you can now use the other eloquent available commands here to also add the given conversation which would be something like:

$camper->myPivotCamperSessionModel->conversations()->create();

as for the second question you can load the pivot data using the withPivot method so this would be something like Camper::withPivot('camperSession') and now you can directly access your pivot data directly from the loaded $camper instance from the attribute camperSession

ddoddsr's avatar

@jjoek

Right after I do this

$camper->sessions()->attach($session_id);
$camper->sessions()->updateExistingPivot($session_id, [
    'booking_email'        => $camper->email,
    'session_type'        => $camper->session_type,
]);

I want to associate a conversation to the CamperSession

ddoddsr's avatar

In some cases I have the CamperSession models and can reach everything but in a few cases I know the camper_is and the sessions_id and need to associate a conversation to the CamperSession (ID)

Please or to participate in this conversation.