title: How to fix error Base table or view not found: 1146 Table laravel relationship table?
body: <p>I am a new of laravel I try to create relationship many to many between table,My problem when I am insert data in to database I got errors</p>
  
  <p>QueryException in Connection.php line 713: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'learn.category_posts' doesn't exist (SQL: insert into <code>category_posts</code> (<code>category_id</code>, <code>posts_id</code>) values (4, ))</p>
  
  <p>can anyone help me pls . and here below is my migrate and code:</p>
  
  <p>2016_08_04_131009_create_table_posts.php</p>
  
  <pre><code>public function up()
  {
      Schema::create('posts', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;text('title');
          $table-&gt;text('body');
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>2016_08_04_131053_create_table_categories.php</p>
  
  <pre><code>public function up()
  {
      Schema::create('categories', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>2016_08_04_131413_create_table_category_posts.php</p>
  
  <pre><code>public function up()
  {
      Schema::create('category_post', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('category_id')-&gt;unsigned();
          $table-&gt;integer('post_id')-&gt;unsigned();
          $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onUpdate('cascade')-&gt;onDelete('cascade');
          $table-&gt;foreign('post_id')-&gt;references('id')-&gt;on('posts')-&gt;onUpdate('cascade')-&gt;onDelete('cascade');
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>and my model Posts.php </p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Posts extends Model
  {
      protected $table = 'posts';
  
      public function categories()
      {
          return $this-&gt;belongsToMany('App\Category');
      }
  }
  </code></pre>
  
  <p>Category.php</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Category extends Model
  {
      protected $table = 'categories'; 
  
      public function posts()
      {
          return $this-&gt;belongsToMany('App\Posts');
      }   
  }
  </code></pre>
  
  <p>My PostsController.php</p>
  
  <pre><code>public function create()
  {
      $categories = Category::all();
      return view('create',compact('categories'));
  }
  
  public function store(Request $request)
  {
     $post = new Posts;
     $post-&gt;title = $request-&gt;title;
     $post-&gt;body = $request-&gt;body;
     $post-&gt;categories()-&gt;attach($request-&gt;categories_id);
  
      return redirect()-&gt;route('posts.index');
  }
  </code></pre>
  
  <p>My View create.blade.php</p>
  
  <pre><code>{!!Form::open(array('route' =&gt; 'store', 'method' =&gt; 'POST'))!!}
  
      {{Form::text('title')}}&lt;br&gt;
      {{Form::textarea('body')}}&lt;br&gt;
      &lt;select name="categories_id" multiple&gt;
          @foreach ($categories as $category)
              &lt;option value="{{ $category-&gt;id }}"&gt;{{ $category-&gt;name }}&lt;/option&gt;
          @endforeach
      &lt;/select&gt;
      &lt;br&gt;
      {{Form::submit('submit')}}
  
  {!!Form::close()!!}
  </code></pre>