Tucker-Eric / EloquentFilter

An Eloquent Way To Filter Laravel Models And Their Relationships
http://tucker-eric.github.io/EloquentFilter
MIT License
1.72k stars 120 forks source link

Access model class #168

Closed Zzombiee2361 closed 2 years ago

Zzombiee2361 commented 2 years ago

I need the model class in my filter class to get the table name. For example I have multiple table with similar status column, so I make a StatusFilter trait to all filter class that need to filter status. This would be fine unless I join a table also with status column in it, so for that case I need to include the table name.

trait StatusFilter {
    public function status($status) {
        $table = (new $this->model)->getTable(); // I need the model class here
        $this->where("{$table}.status", $status);
    }
}
Tucker-Eric commented 2 years ago

You have access to the underlying QueryBuilder so you should be able to do:

trait StatusFilter {
    public function status($status) {
        $table = $this->query->getModel()->getTable();
        $this->where("{$table}.status", $status);
    }
}
Zzombiee2361 commented 2 years ago

Ah I didn't know about the getModel() method. And I could use get_class() if I really need the class