Laravel Eloquent Query Builder Default Where Condition

0 votes

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.

Dec 3, 2020 in Laravel by kartik
• 37,520 points
3,936 views

1 answer to this question.

0 votes

Hello,

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.

answered Dec 3, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

What are the query builder in Laravel?

Hey, Laravel's database query builder provides a convenient, ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,840 points
1,181 views
0 votes
1 answer

How to Make Laravel Eloquent “IN” Query?

Hello @kartik, Here is how you do in ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,840 points
1,043 views
0 votes
1 answer

How can I build a condition based query in Laravel?

Hello @kartik, Try this: $query = DB::table('node'); if ($published == ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,840 points
712 views
0 votes
1 answer

How to simplify this Laravel PHP code to one Eloquent query?

Hello @kartik, You create realtionship between both table friend and user in ...READ MORE

answered Dec 1, 2020 in Laravel by Niroj
• 82,840 points
994 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,090 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,019 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,951 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,894 views
0 votes
1 answer

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

Hii, You can use Conditions using Array: $users = User::where([ ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,840 points
16,767 views
0 votes
1 answer

How to alias a table in Laravel Eloquent queries using Query Builder?

Hello @kartik, You can use less code, writing ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,840 points
10,588 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP