2017-05-31 87 views
1

我有三个模型,我创建了Post和Comment以及User。我试图在两者之间建立一种关系。评论和发布他们都有一个字段“user_id”匹配用户表上的ID。下面是关系,我想设置:Laravel belongsTo关系不起作用

Comment.php:

<?php 

namespace App; 


class Comment extends Model 
{ 
    // 
    public function post(){ 

     return $this->belongsTo(Post::class); 
    } 



    public function user(){ 

     return $this->belongsTo(User::class); 
    } 
} 

post.php中:

<?php 

namespace App; 

class Post extends Model 
{ 

    public function comments(){ 

     return $this->hasMany(Comment::class); 
    } 

    public function addComment($body, $name, $email){ 




     $this->comments()->create(compact('body','name','email')); 
    } 

     public function user(){ // $comment->user->name 

     return $this->belongsTo(User::class); 
    } 
} 

user.php的现在

<?php 

namespace App; 
use App\Post; 

use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 
    use Notifiable; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'name', 'email', 'password', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

    public function post(){ 

     return $this->hasMany(Post::class); 
    } 



} 

你可以看到一条评论属于一个用户,一条帖子属于一个用户。

但是,当我尝试通过修补程序创建一个新的职位(没有用户创建数据库或登录用户)我可以创建一个没有问题的职位;

这告诉我,一个职位必须有一个用户和一个评论的关系必须有一个用户以及它不工作!任何想法如何解决这个问题?

迁移:

create_posts_migration:

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreatePostsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('posts', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('user_id'); 
      $table->string('title'); 
      $table->text('body'); 
      $table->string('image'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('posts'); 
    } 
} 

create_comment_migration:

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateCommentsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('comments', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('post_id'); 
      $table->integer('user_id'); 
      $table->string('email'); 
      $table->string('name'); 
      $table->string('body'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('comments'); 
    } 
} 
+0

请显示您的User.php文件。 –

+0

我编辑帖子,让它现在在那里 –

+0

检查您是否在'posts'表中添加了'user_id'列。 –

回答

0

添加外键约束中迁移创建一个帖子的时候检查有效的用户,并检查有效用户和创建评论时的帖子。这应该可以解决你的问题。

Schema::create('posts', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->unsignedInteger('user_id'); 
    $table->string('title'); 
    $table->text('body'); 
    $table->string('image'); 
    $table->timestamps(); 

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

Schema::create('comments', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->unsignedInteger('post_id'); 
    $table->unsignedInteger('user_id'); 
    $table->string('email'); 
    $table->string('name'); 
    $table->string('body'); 
    $table->timestamps(); 

    $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); 
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
}); 

根据您的需要更改onDelete选项。当父母被删除时,cascade将删除子女关系。

+0

SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败 –

+0

@ L.Kelmendi是不是你的文章的全部点?它按预期工作。创建评论时,您现在必须在创建帖子和有效帖子以及用户时设置有效用户。您在模型中设置的关系用于在您使用雄辩创建它们时适当地获取和分配外键。 – Sandeesh

+0

是的,它是;我的整个观点是吹一个错误。不过,我一直在为用户进行注册/登录。所以我登录,很兴奋创建的帖子,但它吹了同样的错误sqlstate [23000] –