简体   繁体   English

如何使用array_push在PHP / Laravel中通过数据库查询结果在foreach循环中添加键值对以标记返回的每一行?

[英]How can I use array_push to add a key value pair in a foreach loop through database query results in PHP/Laravel to flag each row returned?

I have a function which retrieves all comments for a post when a show posts button is clicked. 我有一个函数,可以在单击“显示帖子”按钮时检索该帖子的所有评论。 The comments are then displayed using Javascript and what I am looking to do is to add a key value pair to the array which will tell my javascript if the comment owner is the logged in user or not, thus allowing me to then add edit and delete buttons to that user's posts only. 然后使用Javascript显示注释,而我要执行的操作是向数组添加键值对,这将告诉我的JavaScript注释所有者是否为登录用户,从而允许我添加编辑和删除操作按钮仅指向该用户的帖子。

This may or may not be the best way to make this comparison but it's a way I cooked up in my infinite inexperience so I can use javascript to show the comments and allow editing/deletion of the comment. 这可能是比较的最佳方法,但也可能不是最好的方法,但这是我在无限的经验中积累的一种方法,因此我可以使用javascript显示注释并允许编辑/删除注释。 I know I could add a column my_posts to the DB table and keep this empty, then use array_push to add the appropriate value to this field but is the way I've tried here possible? 我知道我可以在数据库表中添加一列my_posts并将其保留为空,然后使用array_push向该字段添加适当的值,但是我在这里尝试过的方法可行吗?

my function is here: 我的功能在这里:

public function postGetComments(Request $request) {
     $post = $request['postid'];
     $comments = DB::table('comments')
             ->join('users', 'users.id', '=', 'comments.user_id')
             ->select('comments.*', 'users.username')
             ->where('post_id', '=', $post)
             ->orderby('created_at', 'desc')
             ->get()
             ;
     foreach ($comments->get() as $comment) {
                $user = Auth::user();
                if ($user->id == $comment->user_id) {
                    $comment['my_post'] = 'true';
                } else {
                    $comment['my_post'] = 'false';
                }
     }
     return response()->json(['commentsForPost' => $comments]); 
}

I am getting an error as there is a problem with my foreach loop at the end. 我收到一个错误,因为我的foreach循环最后存在问题。 Without this loop the query retrieves and displays all comments as designed. 没有此循环,查询将检索并按设计显示所有注释。 I'm very new to Laravel (using 5.2) and I'm wondering what I've done wrong while trying to push the key my_post, compare the comments.user_id to the user.id and add the value true/false as appropriate to the array? 我是Laravel的新手(使用5.2),我想知道我在尝试按下键my_post时做错了什么,将comment.user_id与user.id进行比较,并在适当时添加值true / false数组? Thanks! 谢谢!

You have already run the get() method on your $comments query. 您已经在$ comments查询上运行了get()方法。 Change your foreach loop to this 将您的foreach循环更改为此

foreach ($comments as $comment) {
                $user = Auth::user();
                if ($user->id == $comment->user_id) {
                    $comment['my_post'] = 'true';
                } else {
                    $comment['my_post'] = 'false';
                }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM