Laravel'很多很多'用户关联多个帖子?

时间:2018-02-16 03:50:31

标签: laravel foreign-keys posts

在PostsController中

public function store()
    {
        $this->validate(request(), [
            'title' => 'required',
            'body' => 'required'
        ]);

        auth()->user()->publish(
            new Post(request(['title', 'body']))
        );

        return redirect('/');
    }

在user.php中

 public function posts()
    {
        return $this->belongsToMany(Post::class);
    }

    public function publish(Post $post)
    {
        $this->posts()->save($post);
    }

事情是在登录后我看到所有帖子只与登录用户关联但我想要很多帖子到很多用户关系

1 个答案:

答案 0 :(得分:0)

用户模型必须像这样

class User extends Authenticatable
{
       public function Posts()
       {
           return $this->belongsToMany('App\Post');
       }
}

帖子模型必须是这样的。

class Post extends Model
{

        public function users()
        {
            return $this->belongsToMany('App\User');
        }
}

用户的迁移类

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
       Schema::create('users', function (Blueprint $table) {
                  $table->increments('id');
                  $table->string('name');
                  $table->string('email')->unique();
                  $table->string('password');
                  $table->rememberToken();
                  $table->timestamps();
              });
    }

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

Post的迁移类

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

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

你也需要一个数据透视表。表名必须是 - user_post

class CreatePostUser extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     * user_post
     */
    public function up()
    {
        Schema::create('post_user', function(Blueprint $table)
        {
                  $table->integer('user_id')->unsigned()->nullable();
                            $table->foreign('user_id')->references('id')
                                ->on('users')->onDelete('cascade');

                            $table->integer('post_id')->unsigned()->nullable();
                            $table->foreign('post_id')->references('id')
                                ->on('posts')->onDelete('cascade');

            $table->timestamps();
        });
    }

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

请参阅此链接了解更多详情 - https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

我希望你觉得这很有用!