Sidebar sorting issue (侧边栏排序问题)

huangapple go评论95阅读模式
英文:

Sidebar sorting issue

问题

I take 3 posts on to the main home page to display them and i know some of them have weird dates going back to 1987, the idea is to get the latest 4 posts into the home page but on the sidebar im not seeing the truth.

这是代码以及latestPost变量,它负责在页面中心显示一篇文章,我主要讨论的是侧边栏上的文章。

我没有看到任何错误,但我想知道为什么例如“毁灭3”没有显示在那里,因为那篇文章比“毁灭2”更新,可以在这里查看日期列表:https://imgur.com/a/uf9ugGN,这里是主页上的情况:https://imgur.com/JaTwL9V,所以像我之前说的,“毁灭3”应该是排在最前面的文章,对吗?

如何在侧边栏和主页的中心迭代文章,首先检查$latestPost,然后底部是最新的3篇文章的迭代,但不包括“主要最后一篇文章”。

对于屏幕截图(https://imgur.com/a/uf9ugGN),它显示以下数值:

发布日期:“2023年6月4日” | 用户:“guarpig” | 更新日期:“2023年6月5日”

我已经尝试在index()函数中使用“created_at”字段而不是“published_at”。我需要确保文章的published_at日期在侧边栏上是准确的。我的目标是在主页上显示4篇文章,至少现在是这样,最新发布的文章位于中心,之前发布的文章位于侧边栏。

在从函数中移除get和take之后,我将查询从index()中提取并在MySQL上运行了它:

"select * from `posts` where `active` = ? and date(`published_at`) <= '2023-06-05 15:00:00' order by `published_at` desc"
mysql> SELECT * FROM `posts` where `active` = 1 and date(`published_at`) <= '2023-06-05 15:00:00' order by `published_at` desc;
+----+
| id | title                                                  | slug                                              | thumbnail                                                                                    | body                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | active | published_at        | user_id | created_at          | updated_at          | meta_title | meta_description    |
+----+
| 17 | doom 3                                                 | doom-3                                            | 1b8whd1HfrFbLA3sFne8EQK51kdycw-metaZHVjdGFwZS0wMi5wbmc=-.png                                 | &lt;p&gt;yea&lt;/p&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |      1 | 2023-06-05 08:00:00 |       1 | 2023-06-05 16:13:55 | 2023-06-05 16:54:39 | NULL       | NULL                |
| 18 | doom 6                                                 | doom-6                                            | SYrdpa1ZcI0scVaIkHViqq6z4Tn1BI-metaY3JhY2tlZF9pYXR0ZXJuX3RleHR1cmVfYmFja2dyb3VuZC5qcGc=-.jpg | &lt;p&gt;donde aparece este post? y como se ve&lt;/p&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |      1 | 2023-06-05 00:00:00 |       1 | 2023-06-05 16:14:40 | 2023-06-05 17:43:08 | NULL       | my meta description |
| 16 | doom metal 2                                           | doom-metal-2                                      | GPkO6NnUkJeSvo5lFKFG29WJd5AKWb-metad2ViLmpwZw==-.jpg                                         | &lt;p&gt;doom metal active&lt;/p&gt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |      1 | 2023-06-04 00:00:00 |       1 | 2023-06-05 01:09:02 | 2023-06-05 01:09:02 | NULL       | NULL                |
| 14 | Alice! when she first saw the Mock Turtle: &#39;crumbs.    | alice-when-she-first-saw-the-mock-turtle-crumbs   | ECEvIh07WrKg4FzGwQA4ztxKXOzUbb-metaY3JhY2tlZF9pYXR0ZXJuX3RleHR1cmVfYmFja2dyb3VuZC5qcGc=-.jpg | How queer everything is queer to-day.&#39; |      1 | 2022-06-05 11:47:38 |       1 | 2023-06-02 04:42:47 | 2023-06-05 02:18:32 | NULL       | NULL                |
| 13 | Cat in a melancholy tone. &#39;Nobody seems to like her.   | cat-in-a-melancholy-tone-nobody-seems-to-like-her | v98NtC5ESyuHZLhfkNReFwLawtVHtC-metaZHVjdGFwZS0wMi5wbmc=-.png                                 | Seven looked up eagerly, half hoping she might as well she might, what a long way. |      1 | 2014-08-21 11:17:02 |       1 | 2023-06-02 04:42:47 | 2023-06-05 15:58:25 | NULL       | NULL                |
| 12 | Why, I do wonder what Latitude or Longitude either.    | why-i-do-wonder-what-latitude-or-longitude-either | jZBxIwXcS0jeyzg11fnIcNS95ARbgN-metaZ3JpZC1jb2xzLTIuanBn-.jpg                                 | Alice as she could do to hold it.  |      1 | 2012-09-01 00:47:05 |       1 | 2023-06-02 04:42:47 | 2023-06-05 00:17:14 | NULL       | NULL                |
| 15 | Five! Don&#39;t go splashing paint

<details>
<summary>英文:</summary>

I take 3 posts on to the main home page to display them and i know some of them have weird dates going back to 1987, the idea is to get the latest 4 posts into the home page but on the sidebar im not seeing the truth. 

This is the code along with the latestPost variable which takes care of showing a post in the center of the page, what im talking about is mainly the posts on the sidebar.

public function index()
{
    $latestPost = Post::latest()-&gt;first();

    $posts = Post::query()
        -&gt;where(&#39;active&#39;, &#39;=&#39;, 1)
        -&gt;whereDate(&#39;published_at&#39;, &#39;&lt;&#39;, Carbon::now())
        -&gt;orderBy(&#39;published_at&#39;, &#39;desc&#39;)
        -&gt;take(3)-&gt;get();
    return view(&#39;home&#39;, compact(&#39;posts&#39;, &#39;latestPost&#39;));
}
Im not seeing any errors but im wondering why for example &quot;doom 3&quot; is not presented there since that post is more recent than &quot;doom 2&quot;, see here for the list of dates: https://imgur.com/a/uf9ugGN, and here&#39;s a pic of where things are on the main page: https://imgur.com/JaTwL9V, so like i said previously, &quot;doom 3&quot; should be the post on top, right?

how im iterating the posts on the sidebar and the center of the main page, first it checks for the $latestPost and then at the bottom is for the iteration of the latest 3 posts, but excluding the &quot;main last post&quot;

<x-app-layout meta-description="Sick Of Metal blog for the metalhead with rock and heavy metal news and much more">
<div class="md:col-start-2 md:col-end-6 lg:col-start-2 lg:col-end-4 md:mx-auto">
<article class="relative">
<a href="{{ route('view', $latestPost) }}">
<img src="{{ $latestPost->getThumbnail() }}" alt="">
<div class="absolute bottom-0 left-0 right-0 px-4 py-2 bg-gradient-to-t from-black">
<h1 class="font-bold">LATEST NEWS</h1>
<h3 class="text-xl text-white font-bold">
{{ $latestPost->title }}
</h3>
<p class="mt-2 text-sm text-gray-300">
{{ $latestPost->shortBody() }}
</p>
</div> <!-- latest news end -->
</a>
</article>
</div> <!-- latest articles end -->

&lt;div class=&quot;md:col-start-1 md:row-start-1 md:mx-auto lg:w-2/3&quot;&gt;
    &lt;h1 class=&quot;font-bold px-4&quot;&gt;LATEST NEWS&lt;/h1&gt;
    @foreach ($posts as $post)
        &lt;x-post-item :post=&quot;$post&quot;&gt;&lt;/x-post-item&gt;
    @endforeach
&lt;/div&gt; &lt;!-- latest news list end --&gt;

&lt;x-sidebar/&gt;

</x-app-layout>


post-item.blade.php

<article class="shadow my-4 px-4 py-3 flex-col">
<a href="{{ route('view', $post) }}">
<img class="pr-2" src="{{ $post->getThumbnail() }}">
</a>
<div>
@foreach($post->categories as $category)
<a href="#" class="text-blue-700 text-sm font-bold uppercase pb-4">
{{ $category->title }}
</a>
@endforeach
</div>
<a href="{{ route('view', $post) }}">
<h3 class="text-xl font-bold">
{{$post->title}}
</h3>
</a>
</article>


As for the screenshot ( https://imgur.com/a/uf9ugGN ), its showing these values:

published at: &quot;june 4 2023&quot; | user: &quot;guarpig&quot; | updated at: &quot;jun 5 2023&quot;

Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title', 2048);
$table->string('slug', 2048);
$table->string('thumbnail', 2048)->nullable();
$table->longText('body');
$table->boolean('active');
$table->datetime('published_at')->nullable();
$table->foreignIdFor(\App\Models\User::class, 'user_id');
$table->timestamps();
});


I have tried using the &quot;created_at&quot; field instead of &quot;published_at&quot; on the ```index()``` function. I need to ensure that the published_at dates of the posts are accurate on the sidebar. My goal is to show 4 posts, for now at least, on the main homepage. Have the latest the just published post at the center, and the following posts previous to the latest on the side. 

After removing get and take from the function I took the query from the ```index()``` and ran it on MySQL:

"select * from posts where active = ? and date(published_at) < ? order by published_at desc"



mysql> SELECT * FROM posts where active = 1 and date(published_at) <= '2023-06-05 15:00:00' order by published_at desc;
+----+
| id | title | slug | thumbnail | body | active | published_at | user_id | created_at | updated_at | meta_title | meta_description |
+----+
| 17 | doom 3 | doom-3 | 1b8whd1HfrFbLA3sFne8EQK51kdycw-metaZHVjdGFwZS0wMi5wbmc=-.png | <p>yea</p> | 1 | 2023-06-05 08:00:00 | 1 | 2023-06-05 16:13:55 | 2023-06-05 16:54:39 | NULL | NULL |
| 18 | doom 6 | doom-6 | SYrdpa1ZcI0scVaIkHViqq6z4Tn1BI-metaY3JhY2tlZF9wYXR0ZXJuX3RleHR1cmVfYmFja2dyb3VuZC5qcGc=-.jpg | <p>donde aparece este post? y como se ve</p> | 1 | 2023-06-05 00:00:00 | 1 | 2023-06-05 16:14:40 | 2023-06-05 17:43:08 | NULL | my meta description |
| 16 | doom metal 2 | doom-metal-2 | GPkO6NnUkJeSvo5lFKFG29WJd5AKWb-metad2ViLmpwZw==-.jpg | <p>doom metal active</p> | 1 | 2023-06-04 00:00:00 | 1 | 2023-06-05 01:09:02 | 2023-06-05 01:09:02 | NULL | NULL |
| 14 | Alice! when she first saw the Mock Turtle: 'crumbs. | alice-when-she-first-saw-the-mock-turtle-crumbs | ECEvIh07WrKg4FzGwQA4ztxKXOzUbb-metaY3JhY2tlZF9wYXR0ZXJuX3RleHR1cmVfYmFja2dyb3VuZC5qcGc=-.jpg | How queer everything is queer to-day.' | 1 | 2022-06-05 11:47:38 | 1 | 2023-06-02 04:42:47 | 2023-06-05 02:18:32 | NULL | NULL |
| 13 | Cat in a melancholy tone. 'Nobody seems to like her. | cat-in-a-melancholy-tone-nobody-seems-to-like-her | v98NtC5ESyuHZLhfkNReFwLawtVHtC-metaZHVjdGFwZS0wMi5wbmc=-.png | Seven looked up eagerly, half hoping she might as well she might, what a long way. | 1 | 2014-08-21 11:17:02 | 1 | 2023-06-02 04:42:47 | 2023-06-05 15:58:25 | NULL | NULL |
| 12 | Why, I do wonder what Latitude or Longitude either. | why-i-do-wonder-what-latitude-or-longitude-either | jZBxIwXcS0jeyzg11fnIcNS95ARbgN-metaZ3JpZC1jb2xzLTIuanBn-.jpg | Alice as she could do to hold it. | 1 | 2012-09-01 00:47:05 | 1 | 2023-06-02 04:42:47 | 2023-06-05 00:17:14 | NULL | NULL |
| 15 | Five! Don't go splashing paint over me like that!' By. | five-dont-go-splashing-paint-over-me-like-that-by | KX9o6eLkS4HmuyeCnwDgJpnkHvtwap-metaZ3JpZC5qcGc=-.jpg | Hatter: 'let's all move one place on.' He moved on as he shook both his shoes on. 'I'm on the breeze that followed them, the melancholy words:-- 'Soo--oop of the. | 1 | 1987-01-24 22:40:46 | 1 | 2023-06-02 04:42:47 | 2023-06-05 00:16:12 | NULL | NULL |
+----+
7 rows in set (0.01 sec)


</details>


# 答案1
**得分**: 0

使用```where```而不是```whereDate```解决了无法在侧边栏看到我的帖子的问题。

使用```-&gt;whereDate('published_at', '&lt;', now())```的意思是:

1. 查看published_at列,但忽略时间戳 - 只看日期(这是日期A)
2. 将该日期与今天的日期进行比较,再次忽略时间戳(这是日期B)
3. 仅获取日期A小于日期B的行

示例:如果我今天创建了2篇文章,那么在过去的一天内,这两篇文章都不会显示在侧边栏中。这就是为什么我不能在侧边栏中看到“doom 3”的原因。

另一个更改,使用```$posts->shift();``` 专门选择最新创建的资源,这就是我在那里获取最新帖子的原因。

https://laravel.com/docs/10.x/collections#method-shift

<details>
<summary>英文:</summary>

Utilizing ```where``` instead of ```whereDate``` solved the issue of not being able to see my posts on the sidebar.

Using ```-&gt;whereDate(&#39;published_at&#39;, &#39;&lt;&#39;, now())``` means:

1. Look at the published_at column, but ignore the timestamp – only look at the date (that’s Date A)
2. Compare that date to today’s date, again ignoring the timestamp (that’s Date B)
3. Only fetch rows where Date A is less than Date B

Example: If i created 2 posts today, neither one of those two posts would show up today until a day had gone by. That is why I wasn&#39;t able to see &quot;doom 3&quot; in the sidebar. 

public function index()
{
$posts = Post::query()
->where('active', '=', 1)
->where('published_at', '<', Carbon::now())
->orderBy('published_at', 'desc')
->take(4)
->get()
;

$latestPost = $posts-&gt;shift();

return view(&#39;home&#39;, compact(&#39;posts&#39;, &#39;latestPost&#39;));

}

Another change, the use of ```$posts-&gt;shift();``` exclusively selects the latest resource created, which is what i got there to get the latest post.

https://laravel.com/docs/10.x/collections#method-shift

</details>



huangapple
  • 本文由 发表于 2023年6月6日 04:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定