mercredi 15 août 2018

Equivalent to 'where()' on custom attributes

I know that with eloquent, when using where() like

$user = $user->where('age', 21);

it searches the database column. I need to be able to use something similar to where, but on a custom attribute. I know this is not actually do-able, i'm just looking for alternative solutions/suggestions.

I know I can create the data with a custom sql query so it does exist as a column and just use the where on that, but i'm creating a large filtering system so the SQL query would be far too complex.

Are there any solutions which are similar and can achieve the same thing as where() but with a custom attribute?

For example, in a perfect world this code would work

model

public function getFooAttribute(){
   if($this->age >= 21){
      return 'young-adult';
   }
}

controller

$user = $user->where('foo','young-adult')->get();

The questions i have seen are all before 2015, so I just want to know if there are any sort of solutions todays version of laravel?

Open to any suggestions.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire