Last active
March 26, 2019 16:19
-
-
Save DarkGhostHunter/20fe2b0efada92146a559f130d2b44e4 to your computer and use it in GitHub Desktop.
Create a Global scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Scopes; | |
| use Illuminate\Database\Eloquent\Scope; | |
| use Illuminate\Database\Eloquent\Model; | |
| use Illuminate\Database\Eloquent\Builder; | |
| class SelectConstraintScope implements Scope | |
| { | |
| /** | |
| * Apply the scope to a given Eloquent query builder. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $builder | |
| * @param \Illuminate\Database\Eloquent\Model $model | |
| * @return void | |
| */ | |
| public function apply(Builder $builder, Model $model) | |
| { | |
| // First, check if there are columns already set in the query being built by the dev. | |
| // If the columns are empty, it means we are issuing a "SELECT *", so to avoid that | |
| // and querying the whole row, we will just simply put all the columns except one. | |
| if (empty($builder->getQuery()->columns) { | |
| $builder->select([ | |
| $model->getKeyName(), | |
| 'filename', | |
| 'path', | |
| 'service', | |
| 'format', | |
| 'quality', | |
| // 'closed_captions', // <-- We don't need this most of the time. | |
| $model->getCreatedAtColumn(), | |
| $model->getUpdatedAtColumn() | |
| ]); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment