Last active
August 30, 2015 07:37
-
-
Save jgrossi/9603068 to your computer and use it in GitHub Desktop.
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
<?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(); |
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