Rasel
Rasel

Reputation: 3

Laravel Eloquent Where Condition Not Work

I have a Table "items" with total no of data row = 11094 and there are 3 types of "status" (pending, publish & reject)

On MySQL Query

SELECT * FROM items WHERE status = 'pending' returns 1 row
SELECT * FROM items WHERE status = 'publish' returns 11091 rows
SELECT * FROM items WHERE status = 'reject' returns 2 rows

NB: Sum of the individual query result is equal to total no of rows.

But In Laravel (5.4) Controller

$items = Items::where( 'status', '=', 'publish' )->paginate(40); returns 11091 rows

$items = Items::where( 'status', '=', 'reject' )->paginate(40); returns 0 rows (Though 2 Data Available in Table)

$items = Items::where( 'status', '=', 'pending' )->paginate(40); returns 0 rows (Though 1 Data Available in Table)

I can’t figure out what’s is the problem actually. Please Help.

Upvotes: 0

Views: 492

Answers (1)

mrhn
mrhn

Reputation: 18916

Your query is the following.

select * from items where status = ? and items.deleted_at is null and status = ?

There is two statuses in the where, since your code clearly only has one.

$items = Items::where( 'status', '=', 'publish' )->paginate(40);

I'm fairly certain that somewhere in your code, you have a global scope, that filters by the status is publish. Find these and figure out how to solved it from there.

If you do the following, i will highly like believe your logic would work.

$items = Items::withoutGlobalScopes()->where( 'status', '=', 'reject' )->paginate(40);

$items = Items::withoutGlobalScopes()->where( 'status', '=', 'pending' )->paginate(40);

Upvotes: 1

Related Questions