21

I have News model, when i query news, i want it brings news where status = 1 as default.

News::all(); // select * from news where status = 1
News::where('anotherColumn',2)->get(); // select * from news where status = 1 and where category = 2

Is this possible? What i want is so similar to soft delete feature (it gets where deleted_at is not null and if all data is wanted withTrashed function can be used).

I looked docs but i couldn't find anything helpful. Also, i tried to handle it in construct at News model but it didn't worked either.

Thanks.

2
  • Can't you just extend the parent class and overwrite the where function. (This is the part where you could add your custom logic...)
    – Kenny
    Jun 30, 2014 at 7:21
  • 1
    Depends on the framework version. For 4.2+ you simply use global scopes. This is exactly what you need and that's the way softDelete feature works in v4.2+ Jun 30, 2014 at 11:21

3 Answers 3

42

I normally override newQuery() for this. newQuery() is the method that Eloquent use to construct a new query.

class News extends Eloquent {

    public function newQuery($excludeDeleted = true) {
        return parent::newQuery($excludeDeleted)
            ->where(status, '=', 1);
    }

}

Now your News::all() will only output your news with status = 1.

4
  • 2
    This also adds where clause to nested wheres. So to avoid this I am using it like below: public function newQuery($excludeDeleted = true) { $query = parent::newQuery() if(!$this->dirty) { $query->where(status, '=', 1); } return $query; } This is a dirty shortcut way to handle, but I recommend to use global scope. Sep 10, 2014 at 12:44
  • 1
    Any clues on how to make this work with relationships? I'm facing an issue using it with whereHas. Feb 6, 2016 at 13:04
  • Why do you do "return parent::newQuery($excludeDeleted = true) ->where(status, '=', 1);" instead of just "return parent::newQuery($excludeDeleted) ->where(status, '=', 1);" Seems like the value passed before it's being override. Nov 15, 2016 at 22:02
  • @EmirHerrera Thanks for pointing out. That's actually my mistake. Updated accordingly.
    – Unnawut
    Dec 8, 2016 at 4:46
15

It's been already mentioned but here is a quick example using global scope which might be the best current solution since you wont have to override Eloquent methods and would result into the same behavior but with more control of your model.

Just add this to your model :

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('exclude_deleted', function (Builder $builder) {
        $builder->whereNull('deleted_at');
    });
}

You can also create a child Scope class and reuse it for multiple Models.

For more information, Laravel doc explained pretty much everything about it: https://laravel.com/docs/5.6/eloquent#global-scopes

1
13

I think the closes you'll get, without actually going in to change some core files...

is Query Scope...

Scopes allow you to easily re-use query logic in your models. To define a scope, simply prefix a model method with scope:

class News extends Eloquent {
   public function scopeStatus($query)
    {
     return $query->where('status', '=', 1);
    }
 }

Utilizing that scope

 $news  = News::status()->get();
 $news2  = News::status()->where('anotherColumn',2)->get();

Its not quite what you wanted...but its definitely a little shorter than typing

 News::where('status','=',1)->get();

over and over

3
  • Thanks for answer. I know this feature but it doesn't really help. News/*conditions*/->get() is called at so many places. I don't want to change all this lines. Jun 30, 2014 at 7:43
  • 1
    Doesnt your IDE have a find/replace feature where you could just search for all instances of News:: and update with News::status()->, seems like a simple fix really
    – Kylie
    Jun 30, 2014 at 9:16
  • 4
    Thanks for your help and no offense, but why we use framework, oop language, models, views then? I think forcing such solutions teaches me a lot of things and gives me flexibility while coding. What you said is last solution for me. Jun 30, 2014 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.