Created
March 14, 2016 20:46
-
-
Save stephan-v/c100c34c8585f4d1cfb8 to your computer and use it in GitHub Desktop.
Laravel connect polymorphic table to a pivot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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