Laravel 未检索在处创建的注释的帖子

Laravel 未检索在处创建的注释的帖子,laravel,Laravel,我正在尝试检索在时间为注释创建的_,目前我可以添加注释,它将显示注释的时间,但刷新时时间消失 提交时显示时间 刷新时消失,因此需要在getPosts映射方法中检索,但我不确定如何将其包含在中 有人能给我指出正确的方向吗,我知道这与此有关 这就是我现在拥有的 后置控制器 public function getPosts( ) { $posts = Post::with('user') ->with(['likes' => function (

我正在尝试检索在时间为注释创建的_,目前我可以添加注释,它将显示注释的时间,但刷新时时间消失

提交时显示时间

刷新时消失,因此需要在getPosts映射方法中检索,但我不确定如何将其包含在中

有人能给我指出正确的方向吗,我知道这与此有关

这就是我现在拥有的

后置控制器

public function getPosts( )
{
    $posts = Post::with('user')
                 ->with(['likes' => function ($query) {
                            $query->whereNull('deleted_at');
                            $query->where('user_id', auth()->user()->id);


                      }])
                  ->with(['comments' => function($query) {

                        $query->with('user');


                    }])

                    ->get();

    $data = $posts->map(function(Post $post, Comment $comment )
    { 
        $user = auth()->user();

        if($user->can('delete', $post)) {
            $post['deletable'] = true;
        }

        if($user->can('update', $post)) {
            $post['update'] = true;
        }



        $post['likedByMe'] = $post->likes->count() == 0 ? false : true;
        $post['likesCount'] = Like::where('post_id', $post->id)->get()->count();
        $post['createdAt'] = $post->created_at->diffForHumans();
        $post['createdAt'] = $post->updated_at->diffForHumans();
        // not getting the time for comments
        $comment['comment_createdAt'] = $comment->created_at->diffForHumans();

        return array($post, $comment);
    });



    return response()->json($data); 
}
public function create(Request $request, $post)
{

    $data = request()->validate([
     'comment_body' => 'required|max:1000'
    ]);


    $data['user_id'] = auth()->user()->id;
    $data['name'] = auth()->user()->name;
    $data['post_id'] = $post;
    $post = Comment::create($data);
    // sets a time on a comment instantly im using angular :)
    $data['comment_createdAt'] = $post->created_at->diffForHumans();



    $response = new Response(json_encode($data));
    $response->headers->set('Content-Type', 'application/json'); 

    if(!$response){
        return 'something went wrong';
    }

    return response()->json($data); 


}
评论控制器

public function getPosts( )
{
    $posts = Post::with('user')
                 ->with(['likes' => function ($query) {
                            $query->whereNull('deleted_at');
                            $query->where('user_id', auth()->user()->id);


                      }])
                  ->with(['comments' => function($query) {

                        $query->with('user');


                    }])

                    ->get();

    $data = $posts->map(function(Post $post, Comment $comment )
    { 
        $user = auth()->user();

        if($user->can('delete', $post)) {
            $post['deletable'] = true;
        }

        if($user->can('update', $post)) {
            $post['update'] = true;
        }



        $post['likedByMe'] = $post->likes->count() == 0 ? false : true;
        $post['likesCount'] = Like::where('post_id', $post->id)->get()->count();
        $post['createdAt'] = $post->created_at->diffForHumans();
        $post['createdAt'] = $post->updated_at->diffForHumans();
        // not getting the time for comments
        $comment['comment_createdAt'] = $comment->created_at->diffForHumans();

        return array($post, $comment);
    });



    return response()->json($data); 
}
public function create(Request $request, $post)
{

    $data = request()->validate([
     'comment_body' => 'required|max:1000'
    ]);


    $data['user_id'] = auth()->user()->id;
    $data['name'] = auth()->user()->name;
    $data['post_id'] = $post;
    $post = Comment::create($data);
    // sets a time on a comment instantly im using angular :)
    $data['comment_createdAt'] = $post->created_at->diffForHumans();



    $response = new Response(json_encode($data));
    $response->headers->set('Content-Type', 'application/json'); 

    if(!$response){
        return 'something went wrong';
    }

    return response()->json($data); 


}
html

<div ng-show="comments" id="comments" class="col-md-offset-2  animated fadeIn panel-default" ng-repeat="comment in post.comments">
    <div style="font-size:10px;" id="eli-style-heading" class="panel-heading">
      <a class="link_profile" href="/profile/<% comment.user.name | lowercase %>"><% comment.user.name %></a>
    </div>
    <figure class="my-comment">
        <p> <% comment.comment_body%>

        </p>

        <p><% comment.comment_createdAt %> </p>
          <hr>
    </figure>
</div>




回答您的问题:我假设一篇文章有很多评论,您不能只将文章的创建日期指定给一条评论:

$post['comment_createdAt'] = $post->created_at->diffForHumans();
相反,您应该遍历所有注释并将日期存储在数组中

但是这种方法有点麻烦,因为注释对象已经将
created\u保持在
值。我只需检索日期并在前端格式化即可:

 <p><% comment.created_at | diffForHumans %> </p>
但这将转换您在日期创建的文件,并一直将其检索。一种变体和更好的方法是使用自定义访问器:

class Comment  
{
    public function getCreatedAtHumanDiffedAttribute() 
    { 
        return Carbon::createFromFormat($this->dateFormat, $this->created_at)
                       ->diffForHumans();
    }
}

<p><% comment.created_at_human_diffed %> </p>
类注释
{
公共函数getCreateDatHumanDifferenceDatAttribute()
{ 
返回Carbon::createFromFormat($this->dateFormat,$this->created_at)
->diffForHumans();
}
}

关于他们的更多信息:

API资源 使用。你的用例正是它们的用途


老实说,你的代码有问题。一些评论:


使用
$post['comment_createdAt']=$post->created_at->diffForHumans()的全部要点
只是被
调用,所以我不必为diffForhumans创建过滤器。我知道有些代码看起来很麻烦,但主要问题是检索当时创建的代码。我想你在写答案时付出了很多努力,但我认为我的问题还没有解决。我喜欢红色的指针,谢谢。我在回答的顶部提到了解决方案:您需要迭代
map()
中的注释,获取日期并将其存储在数组中。这就是我想知道的,我需要了解如何在代码中使用map,类似于我所拥有的,这并没有为我提供任何关于如何用我目前拥有的东西来实现它的清晰说明。这也无助于我仍然一无所知