title: Paginate data from relationship model in Laravel
body: <p>Apologies if the title doesn't completely make sense... I wasn't sure if the exact terminology for the linking of models are the associated data in their controllers.</p>
  
  <p>I'm trying to paginate a table with the posts created by a user.</p>
  
  <p>I managed to paginate it previously, without the user_id in the posts table. But I've added the <code>user_id</code> to the <code>posts</code> table, and run the migration which works fine.</p>
  
  <p>The following is my <code>Post</code> model:
          
  
  <pre><code>    namespace App;
  
      use Illuminate\Database\Eloquent\Model;
  
      class Post extends Model
      {
        public function user(){
          return $this-&gt;belongsTo('App\User');
        }
      }
  </code></pre>
  
  <p>And this is my <code>User</code> model:
          
  
  <pre><code>    namespace App;
  
      use Illuminate\Notifications\Notifiable;
      use Illuminate\Foundation\Auth\User as Authenticatable;
  
      class User extends Authenticatable
      {
          use Notifiable;
          protected $fillable = ['name', 'email', 'password',];
  
          protected $hidden = ['password', 'remember_token',];
  
          public function posts() {
            return $this-&gt;hasMany('App\Post');
          }
      }
  </code></pre>
  
  <p>And this is the relevant section of the post controller:
          
  
  <pre><code>    namespace App\Http\Controllers;
  
      use Illuminate\Http\Request;
      use App\Post;
      use App\User;
      use Session;
      use Carbon\Carbon;
  
      class PostController extends Controller
      {
  
        public function index()
        {
  
          $user_id = auth()-&gt;user()-&gt;id;
          $user = User::find($user_id);
  
          return view('posts.index')-&gt;with('posts', $user-&gt;posts);
        }
  </code></pre>
  
  <p>I thought that it would simply be a case of appending <code>-&gt;paginate(5)</code> to the <code>$user</code> in the <code>PostController</code>, but that doesn't appear to work.</p>
  
  <p>I get the following: </p>
  
  <pre><code>Undefined property: Illuminate\Pagination\LengthAwarePaginator::$posts
  </code></pre>
  
  <p>I've tried including the <code>{!! $posts-&gt;links() !!}</code> in the <code>posts.index</code> view before the <code>@foreach</code> but that gives: </p>
  
  <pre><code>Undefined property: Illuminate\Pagination\LengthAwarePaginator::$posts
  </code></pre>
  
  <p>I'm trying to learn Laravel basics by creating this crud application, and have tried combining two tutorials together, but I'm clearly missing something.</p>