Skip to content

Instantly share code, notes, and snippets.

@jgrossi
Last active August 30, 2015 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgrossi/9603068 to your computer and use it in GitHub Desktop.
Save jgrossi/9603068 to your computer and use it in GitHub Desktop.
<?php
class Entry extends Eloquent
{
protected function withPaymentsStatus($status)
{
return $this->whereHas('payments', function($query) {
$query->where('status', $status);
});
}
public function paid()
{
return $this->withPaymentsStatus(1);
}
public function pending()
{
return $this->withPaymentsStatus(2);
}
}
// pra usar no controller
$entries = Entry::withPaymentsStatus(2)->get();
// pq não retornar com o ->get() já??? pq vc pode usar outras coisas tipo:
$entries = Entry::orderBy('created_at', 'DESC')->withPaymentsStatus(3)->get();
@jgrossi
Copy link
Author

jgrossi commented Mar 17, 2014

public function payments($status)
{
    return $this->hasMany('Payment', 'entry_id')
        ->where('status', $status);
}

@jgrossi
Copy link
Author

jgrossi commented Mar 17, 2014

public function payments($status = null)
{
    $return =  $this->hasMany('Payment', 'entry_id');

    if ($status !== null) {
        return $return->where('status', $status);
    }

    return $return;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment