Skip to content

Instantly share code, notes, and snippets.

@ksenijakuc
Last active March 23, 2016 06:16
Show Gist options
  • Save ksenijakuc/2a643c28ec6306756d7d to your computer and use it in GitHub Desktop.
Save ksenijakuc/2a643c28ec6306756d7d to your computer and use it in GitHub Desktop.
laravel orm query: get authors with one general film (films belongsTo authors)
<php
// table authors: id, name
// table films: id, name, general(boolean), author_id
Authors::leftJoin('films as general_film', function ($q) {
$q->on('authors.id', '=', 'general_film.author_id')
->on('general_film.id', '=',
DB::raw('(select id from films WHERE author_id = authors.id ORDER BY general DESC LIMIT 1)'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment