0

Now I get some model with all properties filled except one.

So I want to make a search in a database and see if there is some registry that matches all the properties values, in which case, get the last property value and keep it.

Now im doing a query wit query builder, giving it all where like this:

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

But I want to know if there some way to make a shortcut like...

$instance->get();
1
  • 2
    Please have a look at the Eloquent documentation. Everything you need is there, including examples and common use cases. laravel.com/docs/5.2/eloquent Commented Apr 19, 2016 at 8:45

1 Answer 1

1

Yes you can do this by defining method in your model like this

class YourModel extends Model
{
  public function getFiltered()
  {
    return Model::where('field1',$this->field1)->where('field2',$this->field2)->get();
  }
}

And you can access it like this:

$instance->getFiltered();

You cannot keep function name 'get' because It's already being used in Model which is being extended. But you can change from getFiltered to anything which is not used. Like If the Model was User and the function getFiltered gives user's comments then It can be comments.

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

3 Comments

Nice, sorry for taking to long to answer. Very clean, thats what i was looking for. Thank you :)
Done. Thank you again :)
@Sampudon No problem.

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.