Skip to content

Instantly share code, notes, and snippets.

@jgrossi
Created March 17, 2014 14:11
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/9599844 to your computer and use it in GitHub Desktop.
Save jgrossi/9599844 to your computer and use it in GitHub Desktop.
class Payment extends Eloquent
{
const PAID = 1;
const PENDIND = 2;
public function entry()
{
return $this->belongsTo('Entry', 'entry_id');
}
// ...
}
class Entry extends Eloquent
{
public function payments($status = null)
{
$relation = $this->hasMany('Payment', 'entry_id');
if ($status !== null) {
$relation->where('status', $status);
}
return $relation;
}
}
// Controller
$entries = Entry::payments(Payment::PAID)->get();
@jgrossi
Copy link
Author

jgrossi commented Mar 17, 2014

Pode criar metodos especificos também: tipo "pagamentosPagos":

class Entry extends Eloquent
{

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

}

// Controller
$entries = Entry::paidPayments()->get();

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