Skip to content

Instantly share code, notes, and snippets.

@stephan-v
stephan-v / gist:c100c34c8585f4d1cfb8
Created March 14, 2016 20:46
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();
<?php
$gemeentes = [
'Aa en Hunze',
'Aalburg',
'Aalsmeer',
'Aalten',
'Achtkarspelen',
'Alblasserdam',
'Albrandswaard',