Laravel 'many to many' 用户关联了很多帖子?

Laravel 'many to many' user associates with many 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);
    }

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

用户模型必须是这样的

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

post模型一定是这样的

class Post extends Model
{

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

用户

的迁移class
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');
       }
}

迁移 class 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');
    }
}

你还需要一个支点table。 table 名称必须是 - 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');
    }
}

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

希望你觉得这有用!