I\'m trying to implement an \"approved\' state for a table I have, it\'s pretty straightforward, basically, if the row\'s approve column equals 1; that row should be retrieved,
The closest thing I found is Eloquent query scope.
Even though it requires a minor change in my code(prefixing queries) it still gives me what I'm looking with great flexibility.
Here's an example:
Create a function within the Eloquent child class:
class Post extends Eloquent {
public function scopeApproved($query)
{
return $query->where('approved', '=', 1/*true*/);
}
}
Then simply use it like this:
$approvedPosts = Post::approved()->;
Works perfectly. No ugly repeated WHERE function calls. easy to modify. Much easier to read(approved()
makes much more sense than where('approved', '=', 1)
)