5

I am new in Laravel. I want to make some custom functions in models which is related to database query.

Class A Extends Controller{
  public function view(){
    B::get_user();
  }
}

Class B Extends Model{
  protected $table = "user";

  public function get_user(){
    //Here is my database query
  }
}

How can I use database query in get_user() function? I know this method:

B::table('user')->get();

1 Answer 1

5

You can define query scopes for adding the query on the model as:

public function scopeUser($query)
{
    return $query->where('some_field', 'some_value');
}

Then you can use it in you controller as:

B::user()->get();

Docs

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.