Skip to content
Advertisement

Avoid laravel global scope on specific queries

I have a stuents tablle with 3 columns graduated , leaver and deleted_at.

graduated and leaver are int (1) with null as default value. When a student graduates or leaves the school, the corresponding columns becomes 1.

When i use

JavaScript

I need to only return Student where graduated and leaver are null, so I create two global scopes:

JavaScript

This works for most use cases, but what should I do for some specific queries when I want the students thar are graduated? such as

JavaScript

Advertisement

Answer

If you need to avoid a global scope in a specific query, yuu should use withoutGlobalScope:

JavaScript

I would create a local scope in your Model called graduated and leaver:

JavaScript

Then you will be able to query the data using:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement