mercredi 21 février 2018

How do I add a user_id to a polymorphic relationship?

I have a polymorphic relationship between Posts, Videos and Comments.

What I want to add is who made the relationship between the Posts/Comments or Videos/Comments. Essentially add a userId to the comments table.

Currently I have

user
    id - integer
    name - string

posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string
    user_id - integer

I can not figure out how to use eloquent to achieve what I want. I have followed the docs and added the morph relationships as per the docs but I can't seem to find a way for the user_id to be saved into the comments table as well.

How do I use eloquent to also save the user_id each time the polymorphic relationship is created?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire