-1

I use Accessor for my Model

public function getStatusAttribute($value) {
   switch ($value) {
      case 1:
         return 'Active';
         break;
      default:
         return 'Nonactive';
         break;
   }
}

How can I run a whereRaw based on a value? something like this:

if

$search = 'act';
$user = User::whereRaw("status like '%$search%'");

Is there a way to do this?

I expected, i can get User Data with status by searching similar 'active' or 'nonactive' , not '0' or '1'

6
  • Does this answer your question? eloquent search/where on custom attributes Commented Aug 30, 2023 at 10:16
  • also: stackoverflow.com/questions/55569604/… Commented Aug 30, 2023 at 10:17
  • let me try the answer Commented Aug 30, 2023 at 10:19
  • I don't think that's what I meant Commented Aug 30, 2023 at 10:21
  • make a select filter for active and nonactive and avoid this weird solution. Commented Aug 30, 2023 at 10:22

1 Answer 1

0

You wont be able to do this exactly.

The database does not know about your custom attribute so you will need to do the filtering after all the items come back from the database or do the search yourself in code first. This is what was suggested in the comments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.