提问者:小点点

Laravel属于关系不起作用


我创建了三个模型,分别是Post、Comment和User。我试图在两者之间建立一种关系。Comment和Post都有一个字段“user\u id”,与user表上的id匹配。以下是我正在尝试设置的关系:

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);
    }
}

邮递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);
    }
}

使用者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);
    }



}

现在,您可以看到一个评论属性给用户和一个帖子属性给用户。

然而,当我试图通过tinker创建一个新帖子时(没有在数据库上创建用户或登录用户),我可以创建一个没有问题的帖子;

这告诉我,一篇文章必须有一个用户,一篇评论也必须有一个用户,这种关系不起作用!你知道怎么解决这个问题吗?

迁移:

创建\u帖子\u迁移:

<?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');
    }
}

创建\u注释\u迁移:

<?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');
    }
}

共1个答案

匿名用户

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

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将删除子关系。