spoon
4 years ago

How should I re-name my existing "friends" table to follow Laravel's convention?

Posted 4 years ago by spoon

Hello.

I used to follow my own convention but I decided to restructure my database to follow Laravel's own convention, so things will be more easy to deal with for me. However, I bumped into an issue.

Here's my existing table, how should I rename it and its columns so I won't deal with foreign keys and primary keys while creating a relation on model? There are two user_id's, so I can't give user_id and user_id, I have to use a different name like friend_one and friend_two. Does it matter?

friendship

friend_one (foreign key from users table, user_id)
friend_two (foreign key from users table, user_id)
status
created_at

Here's my current friends() method in my User model.

    public function friends() {
        return $this->belongsToMany('App\Models\User', 'friendship', 'friend_one', 'friend_two');
    }

Please sign in or create an account to participate in this conversation.