Laravel 使用OctoberCMS RainLab博客从不同类别获取组合帖子

Laravel 使用OctoberCMS RainLab博客从不同类别获取组合帖子,laravel,octobercms,octobercms-plugins,Laravel,Octobercms,Octobercms Plugins,我正在使用10月份的Rain Lab Post。 我使用blogPost组件并从单个类别获取帖子没有问题。例如,这是一个部分,其中显示类别中的最后5篇文章 [blogPosts] pageNumber = "{{ :page }}" categoryFilter = "{{ slug }}" postsPerPage = 5 noPostsMessage = "No news in this category" sortOrder = "published_at desc" categoryPa

我正在使用10月份的Rain Lab Post。 我使用blogPost组件并从单个类别获取帖子没有问题。例如,这是一个部分,其中显示类别中的最后5篇文章

[blogPosts]
pageNumber = "{{ :page }}"
categoryFilter = "{{ slug }}"
postsPerPage = 5
noPostsMessage = "No news in this category"
sortOrder = "published_at desc"
categoryPage = 404
postPage = "singlePost"
==
<li> <a href="#">{{category.name}}</a>
    <ul class="drop-down full-width col-5 hover-expand">
        <li class="validation">
            <h2 class="mm-title">{{category.name}}</h2>
        </li>
        {% for post in posts %}
        <li>
            {% for image in post.featured_images|slice(0,1) %}
                <a href="{{ post.url }}"><img src="{{ image.path }}" alt=""></a>
            {% endfor %}
            <h3><a href="{{ post.url }}">{{post.title}}</a></h3>
        </li>
        {% endfor %}
    </ul>
</li>
[博客帖子]
pageNumber=“{{:page}”
categoryFilter=“{{slug}}”
postsPerPage=5
noPostsMessage=“此类别中没有新闻”
sortOrder=“在描述中发布”
类别页=404
postPage=“singlePost”
==
    • {{category.name}
    • {posts%%中的post为%s}
    • {后期特色图片中的图片百分比|切片(0,1)%} {%endfor%}
    • {%endfor%}
  • 但是现在,我在主页上工作,希望显示所有类别的最后一篇文章。每个类别1个,组合5个类别

    有人知道怎么做


    提前感谢

    这样,您必须在页面/布局的代码部分手动获取数据

    function onStart() {
    $first_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 1)->first();
    $second_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 2)->first();
    $third_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 3)->first();
    $fourth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 4)->first();
    $fifth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 5)->first();
    
    $array = $first_category_post->merge($second_category_post);
    $array = $array->merge($third_category_post);
    $array = $array->merge($fourth_category_post);
    $array = $array->merge($fifth_category_post);
    
    $this['posts'] = $array;
    }
    
    另一个解决方案是使用常用的php
    array\u merge

    function onStart() {
    $first_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 1)->first();
    $second_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 2)->first();
    $third_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 3)->first();
    $fourth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 4)->first();
    $fifth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 5)->first();
    
    $array = array_merge($first_category_post->toArray(), $second_category_posts->toArray());
    $array = array_merge($array, $third_category_post->toArray());
    $array = array_merge($array, $fourth_category_post->toArray());
    $array = array_merge($array, $fifth_category_post->toArray());
    
    $this['posts'] = $array->toJson();
    }
    

    这样,您必须在页面/布局的代码部分手动获取数据

    function onStart() {
    $first_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 1)->first();
    $second_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 2)->first();
    $third_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 3)->first();
    $fourth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 4)->first();
    $fifth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 5)->first();
    
    $array = $first_category_post->merge($second_category_post);
    $array = $array->merge($third_category_post);
    $array = $array->merge($fourth_category_post);
    $array = $array->merge($fifth_category_post);
    
    $this['posts'] = $array;
    }
    
    另一个解决方案是使用常用的php
    array\u merge

    function onStart() {
    $first_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 1)->first();
    $second_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 2)->first();
    $third_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 3)->first();
    $fourth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 4)->first();
    $fifth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 5)->first();
    
    $array = array_merge($first_category_post->toArray(), $second_category_posts->toArray());
    $array = array_merge($array, $third_category_post->toArray());
    $array = array_merge($array, $fourth_category_post->toArray());
    $array = array_merge($array, $fifth_category_post->toArray());
    
    $this['posts'] = $array->toJson();
    }
    

    多亏了艾哈迈德·埃萨姆(Ahmed Essam)的第一个建议,我以下一种方式解决了这个问题:

    function onStart(){
       $categories = Db::table('rainlab_blog_categories')->get();
       foreach ($categories as $key=>$category){
          if($category->slug != 'uncategorized'){
             $first = \Rainlab\Blog\Models\Category::with('posts')->where('id',$category->id)->first();
             $data[] = $first->posts[0];
          }
       }
       $this['posts'] = $data;
    }
    
    在模板中,我可以这样使用

    {% for post in posts %}
        <li class="col-md-6 col-sm-6">
            <div id="container">
    
                {% for image in post.featured_images | slice(0,1) %}
                   <div class="thumb"><img src="{{image.path}}" alt=""></div>
                {% endfor %}
    
                <div class="content">
    
                    {% for category in post.categories %}
                        <div class="cat">{{category.name}</div>
                    {% endfor %}
    
                    <h3><a href="{{ post.url }}">{{ post.title )}}</a></h3>
                </div>
            </div>
        </li>
    {% endfor %}
    
    {%for posts in posts%}
    
  • {post.Characterized|U图像中图像的百分比|切片(0,1)%} {%endfor%} {post.categories%中的类别为%s} {{category.name} {%endfor%}
  • {%endfor%}
    多亏了艾哈迈德·埃萨姆的第一个建议,我用下一种方法解决了这个问题:

    function onStart(){
       $categories = Db::table('rainlab_blog_categories')->get();
       foreach ($categories as $key=>$category){
          if($category->slug != 'uncategorized'){
             $first = \Rainlab\Blog\Models\Category::with('posts')->where('id',$category->id)->first();
             $data[] = $first->posts[0];
          }
       }
       $this['posts'] = $data;
    }
    
    在模板中,我可以这样使用

    {% for post in posts %}
        <li class="col-md-6 col-sm-6">
            <div id="container">
    
                {% for image in post.featured_images | slice(0,1) %}
                   <div class="thumb"><img src="{{image.path}}" alt=""></div>
                {% endfor %}
    
                <div class="content">
    
                    {% for category in post.categories %}
                        <div class="cat">{{category.name}</div>
                    {% endfor %}
    
                    <h3><a href="{{ post.url }}">{{ post.title )}}</a></h3>
                </div>
            </div>
        </li>
    {% endfor %}
    
    {%for posts in posts%}
    
  • {post.Characterized|U图像中图像的百分比|切片(0,1)%} {%endfor%} {post.categories%中的类别为%s} {{category.name} {%endfor%}
  • {%endfor%}
    谢谢,你给了我提示,但有一个问题:category\u id字段在另一个表中。有3个表:rainlab\u blog\u posts;rainlab\u blog\u categories;rainlab\u blog\u post\post=\rainlab\blog\Models\post::orderBy('id',desc')->where('category\u id',1)->first();返回错误,因为“category_id”位于rainlab_blog_posts_categories关系表中。谢谢,您给了我提示,但有一个问题:category_id字段位于其他表中。共有3个表:rainlab_blog_posts;rainlab_blog_categories;rainlab_blog_posts_categories;使用:$first_category_post=\rainlab\blog\Models\post::orderBy('id','desc')->where('category_id',1)->first();返回错误,因为'category_id'在rainlab_blog_posts_categories关系表中。