Search code examples
eloquentoctobercmsoctobercms-plugins

OctoberCMS scope not applying


So the scenario here is that i'm trying to create a simple scope in OctoberCMS that will allow me to filter a query with the builder plugins list component. In my model I have defined the scope as such:

public function scopeFirst($query)
{
    return $query->where('practice_name',1);

}

This should just constraint the query to fetch only the records where that value is 1. The Component is recognizing this scope and allowing me to choose it from the drop-down list, as indicated by my index.htm file:

[builderList]
modelClass = "vetadmin\Practicedetails\Models\Practicedetails"
scope = "scopeFirst"
displayColumn = "id"
noRecordsMessage = "No records found"
detailsPage = "-"
detailsUrlParameter = "id"
pageNumber = "{{ :page }}"

Does anybody have any ideas of what could be causing it to not apply the constraint? Currently -all- the records are being returned. The documentation isn't particularly elaborate on this and just suggests you need to define the scope in your plugins model php file (as I've done)

https://octobercms.com/docs/database/model#query-scopes

is the documentation i'm referring to.

Thanks.


Solution

  • Hmm this is little confusing for the larval eloquent model

    I have tested your code and scope

    Then I just realised that scopeFirst can be apply on modal like this $modal->first() <= this is issue its reserved method

    As per my knowledge first [ref: https://laravel.com/docs/5.7/eloquent] is method to get first record from the collection/models. [ So, your scope is treated like that and ended up doing nothing ! ]

    So, just change your scope name like scopeFirstPractice etc.. can solve our issue.

    and just normally use

    [builderList]
    modelClass = "vetadmin\Practicedetails\Models\Practicedetails"
    scope = "scopeFirstPractice"
    

    It should definitely work. [ I have tested. ]

    if any doubt please comment.