I am not sure if this is possible with Laravel eloquent, but I want to create an eloquent where the where
condition would be based on the value of the current table column.
Something like this for illustration purposes:
... if (column_a == 'paid') ->where(function($q) { $q->where('status', '!=', 'failed'); $q->where('status', '!=', 'cancelled'); }) else ->where(function($q) { $q->where('status', '!=', 'authorized'); }) ... ->get();
column_a
is part of the table column that I want to use. I could create 2 separate eloquent with their corresponding conditions and then merge them later but I dont want to go down that route if possible.
I thought of using ->when()
but based on what I understood, ‘->when()’ is used for variable. Sorry if this is something basic but I dont really know how to proceed with this.
PS:
I also tried
->where('column_a', '=', 'paid', function($q) { $q->where(function($q) { $q->where('status', '!=', 'failed'); $q->where('status', '!=', 'cancelled'); }); }) ->where('column_a', '!=', 'notpaid', function($q) { $q->where(function($q) { $q->where('status', '!=', 'authorized'); }); })
But I am getting Object of class Closure could not be converted to string
Advertisement
Answer
I think what you want to do is this :
->where(function($q) { $q->where('column_a', '=', 'paid') ->where('status', '!=', 'failed') ->where('status', '!=', 'cancelled'); }) ->orWhere(function($q) { $q->where('column_a', '!=', 'notpaid') ->where('status', '!=', 'authorized'); })