One to many Definition and Foreign Key

Posted 11 months ago by Opulido

I have seen on tutorials people declaring the foreign key on one to many scenarios and others do not use it. Which is the way to go? Should we always use the foreing key definition, for example:

$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users');

and second question, how to reference the model class in the relationship:

    return $this->hasMany('App\Comment');      This one
    return $this->hasMany(Comment::class);     Or this one

Regards, Omar

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