首页 > 解决方案 > 十月CMS范围不适用

问题描述

所以这里的场景是我试图在 OctoberCMS 中创建一个简单的范围,这将允许我使用构建器插件列表组件过滤查询。在我的模型中,我已将范围定义为:

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

}

这应该只是将查询限制为仅获取该值为 1 的记录。组件正在识别此范围并允许我从下拉列表中选择它,如我的 index.htm 文件所示:

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

有没有人知道什么可能导致它不应用约束?目前 - 所有 - 正在返回记录。文档没有特别详细说明这一点,只是建议您需要在插件模型 php 文件中定义范围(正如我所做的那样)

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

是我指的文档。

谢谢。

标签: eloquentoctobercmsoctobercms-plugins

解决方案


嗯,这对larval eloquent model

我已经测试了您的代码和范围

然后我才意识到scopeFirst可以像这样应用于模态$modal->first()<=this is issue its reserved method

据我所知first[ref: https://laravel.com/docs/5.7/eloquent]是从collection/models. [所以,你的范围被这样对待,最终什么也没做!]

因此,只需更改您的范围名称scopeFirstPractice即可解决我们的问题

并且通常使用

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

它绝对应该有效。【我已经测试过了。]

如有任何疑问,请发表评论。


推荐阅读