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 :)
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);
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community