【问题标题】:Octobercms Component - Limit results on relationOctobercms 组件 - 限制关系的结果
【发布时间】:2019-12-05 18:54:53
【问题描述】:

我有一个包含 2 个组件的插件。一个用于“帖子”,另一个用于“个人资料”。该个人资料属于某个用户,并且有许多“帖子”。

但是,当我访问关系时,它会加载每个帖子。我只想加载 5 然后分页或延迟加载,我该怎么做?

Profile.php 模型

public $hasMany = [
        'posts' => [
            'Redstone\Membership\Models\Post',
            'table' => 'redstone_membership_profiles_posts',
        ]
    ];

public $belongsTo = [
        'user' => [
            'Rainlab\User\Models\User',
        ]
    ];

Post.php 模型

public $belongsTo = [
        'profile' => ['Redstone\Membership\Models\Profile',
        'table' => 'redstone_membership_profiles_posts',
        ]
    ];

Profile.php 组件

protected function loadProfile()
    {
        $id = $this->property('profileUsername');

        $profile = new UserProfile

        $profile = $profile->where(['slug' => $id]);

        $profile = $profile->first();
        return $profile;
    }

profile/default.htm - 组件视图

{% set profile = __SELF__.profile %}
{% set posts = __SELF__.profile.posts %}

 {% for post in posts %}
      <div class="card">
        <div class="card-header">
            <div class="ml-2">
                <div class="h5 m-0">{{ profile.title }}</div>
                  </div>
                  {{ post.published_at|date('M d') }}  
                </div>
              <div class="card-body  text-left">
                    {{ post.content|raw }}
            </div>
        </div>
 {% else %}

 <h1>This user has not made any posts.</h1>

 {% endfor %}

【问题讨论】:

    标签: php octobercms octobercms-plugins


    【解决方案1】:

    你可以使用 OctoberCMS pagination service 或 php 函数做一些事情,或者你可以通过 twig 构建一个函数。

    PHP 使用切片:假设当您调用$profile-&gt;posts 时,您会收到一组帖子。您还可以向 URL 添加一个查询,例如 example.com/profile?q=15 以将 5 更改为 10 15 等。我添加了一个 if 语句来检查以确保输入是数字且大于 5。

    protected function loadProfile()
    {
        $id = $this->property('profileUsername');
    
        $profile = UserProfile::where(['slug' => $id])->first();
    
        if (is_numeric(Input::get('q')) == true && Input::get('q') > 5) {
    
            $profile->posts = $profile->posts->slice(0, Input::get('q'));
    
        } else { 
    
            $profile->posts = $profile->posts->slice(0, 5);
    
        }
    
        return $profile;
    }
    

    Twig using slice:这与 PHP 的方式非常相似,但在 htm 文件中完成。

    PHP -

    protected function getQuery() 
    {
    
        if (is_numeric(Input::get('q')) == true && Input::get('q') > 5) {
            return Input::get('q');
        } else {
            return 5;
        }
    
    }
    

    树枝-

    {% set query = __SELF__.getQuery %}
    {% set profile = __SELF__.profile %}
    {% set posts = __SELF__.profile.posts | slice(0, query) %}
    
     {% for post in posts %}
          <div class="card">
            <div class="card-header">
                <div class="ml-2">
                    <div class="h5 m-0">{{ profile.title }}</div>
                      </div>
                      {{ post.published_at|date('M d') }}  
                    </div>
                  <div class="card-body  text-left">
                        {{ post.content|raw }}
                </div>
            </div>
     {% else %}
    
     <h1>This user has not made any posts.</h1>
    
     {% endfor %}
    

    【讨论】:

    • +1 感谢您提供多个示例!我什至从未考虑过使用对 URL 的查询。我看到了分页选项,但认为这是不可能的,因为为配置文件获取 ->first() 。是否可以仅使用 ->paginate(15) 对关系的结果进行分页?无论哪种方式,我都喜欢 PHP 使用 slice 的想法!
    • 不客气。您可以做的不仅仅是查询以及加载更多按钮或搜索特定帖子的特定内容等。十月提供的分页服务非常好,但确实需要一些学习曲线,我不确定如果你想要的话。
    猜你喜欢
    • 2019-08-16
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多