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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With