Support the ongoing development of Laravel.io →
Eloquent Laravel Database
Last updated 2 years ago.
0

Declare foreign key very necessary for the referential integrity. As for a second question, Laravel official documentation recommended declare in the following form:

public function comments()
{
    return $this->hasMany('App\Comment');
}

Note, a correct form declare foreign key use migration:

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

P.S sorry for my English :)

0

You should try this:

1)

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

2)

public function Comment()
{
      return $this->hasMany('App\Comment');
}

OR

use App\Comment;
 
public function Comment()
 {
       return $this->hasMany(Comment::clss);
 }
Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Omar Pulido opulido Joined 10 Sep 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.