Skip to content

Instantly share code, notes, and snippets.

@stephan-v
Created March 14, 2016 20:46
Show Gist options
  • Save stephan-v/c100c34c8585f4d1cfb8 to your computer and use it in GitHub Desktop.
Save stephan-v/c100c34c8585f4d1cfb8 to your computer and use it in GitHub Desktop.
Laravel connect polymorphic table to a pivot
// it ain't pretty but it works
// grab all the beer_user pivot records from a table and the corresponding commentable records
$comments = \DB::table('users')
->select('beer_user.id', 'comments.comment', 'comments.commentable_id', 'comments.created_at')
->join('beer_user', 'users.id', '=', 'beer_user.id')
->join('comments', 'beer_user.id', '=', 'comments.commentable_id')
->where('beer_user.user_id', $id)
->where('comments.commentable_type', 'App\Beer')
->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment