【问题标题】:Laravel (OctoberCMS) sorting (getting) modelsLaravel (OctoberCMS) 排序(获取)模型
【发布时间】:2021-03-16 19:34:43
【问题描述】:

我正在尝试按组名(在组件中)对课程进行排序。我使用 Ajax 通过 Post 方法获取 group_id 并尝试获取类似的课程,但返回的数组为空。

Schedule.php:

        public $belongsTo = [
            'discipline' => 'Titamik\Cio\Models\Discipline',
            'auditorium' => 'Titamik\Cio\Models\Auditorium',
            'professors' => 'Titamik\Cio\Models\Professor',
            'groups'     => 'Titamik\Cio\Models\Group',
        ];

Group.php:

        public $hasMany = [
                'direction' => 'Titamik\Cio\Models\Direction',
                'schedule' => 'Titamik\Cio\Models\Schedule'
        ];

ScheduleComponent.php:

        public function onSort() {
            $groupId = post('groupid');
            $this->page['schedule'] = Schedule::groups()->where('id', $groupId)->get();
            return $this->page['schedule'];
        }

【问题讨论】:

    标签: php laravel octobercms


    【解决方案1】:

    这应该可以解决您的问题。 Read more here

    $schedules = Schedule::whereHas('groups', function ($query) use ($groupId) {
        $query->where('id', $groupId);
    })->get();
    

    另一个旁注。如果您要通过 ajax 进行排序,则不需要页面变量;它不会在没有页面加载的情况下更新。在这种情况下,只需将值返回时间表。我也会考虑将responses 回复您的请求。

    public function onSort() {
        $groupId = post('groupid');
        $schedules = Schedule::whereHas('groups', function ($query) use ($groupId) {
            $query->where('id', $groupId);
        })->get();
        return $schedules;
    }
    

    【讨论】:

    • 由于会有很多排序,所以我决定不用每次都查询数据库,而是对启动组件时得到的数组进行排序。对此,存在三个问题: 1. 这是正确的决定吗? 2.如何获取与课程相关联的组的id$this->page['schedule'][1]->groups[0] 3.如果我使用更新参数```update:{',为什么通过返回使用数组而不是页面变量更合理schedulecomponent::schedule-table': '#schedule-table-container'},```
    • @Titamik 您从未解释过您正在更新部分内容。为您的第二个和第三个问题打开一个新问题。您的第一个问题是意见,如果您严格追求性能,请获得更好的服务器。 Laravel 在查询数据库方面非常高效。我没有遇到很多过滤器的性能问题。
    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2018-10-12
    • 2021-03-10
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多