php - Laravel 5. Query from a existing model instance? -


now model properties filled except one.

so want make search in database , see if there registry matches properties values, in case, last property value , keep it.

now im doing query wit query builder, giving this:

$query->model::select()->where(field, $instance->field); $query->where(field2, $instance->field2); ... $query->get(); 

but want know if there way make shortcut like...

$instance->get(); 

yes can defining method in model this

class yourmodel extends model {   public function getfiltered()   {     return model::where('field1',$this->field1)->where('field2',$this->field2)->get();   } } 

and can access this:

$instance->getfiltered(); 

you cannot keep function name 'get' because it's being used in model being extended. can change getfiltered not used. if model user , function getfiltered gives user's comments can comments.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -