0

I've opened this discussion . I have the same code that is there, please have a look at that.

I'm getting the comment body on the fly, perfectly fine, But Now I want to fetch the user name and image when the user leaves a comment and show it on the screen below the post description.

Problem

Right now I can get only the user_id, how can I get the user_name and the image?

My comments table where I'm inserting the comments looks like:this.

So how can I get the user_name of the user who is leaving a comment, right now I can only get the user_id

For better understanding, I want to get the user_name below this line in Ajax Request Code

$(".show_comments_"+post_id).append("<div style = 'color:red;'>"+data.msg+"</div>")

You can see I've tried for the name, but I'm unable to get the user_name and image who is leaving a comment.

Note:

I've models that have the perfectly fine relations from posts to users and vice versa.

I'm new to laravel.

Updated :

In Ajax handling Controller I have code something like this :

 public function storeComments(Request $request,Comment $body,$post_id){
   if($request->ajax()){
    $comment = new Comment;
    $comment->user_id =  Auth::user()->id;
    $comment->post_id = $post_id;
    $comment->body = Input::get('body');

    $comment->save();
   return response()->json( ['msg'=>$comment->body,'user_id' =>$comment->user_id] );       

    }


}

I'm trying to load image but I got the following

this is what I got instead of image

Thanks In advance.

contributor
  • 143
  • 3
  • 12
  • In your AJAX handling Controller code, you will have to fetch the users details from 'user_id'. Save those details in a variable or array and then send it back to the AJAX request. – Abhishek Mugal Jun 15 '19 at 08:05
  • Hi, I've updated the post with `controller` you are talking about, what should I add there to get the `name` and the `image` of the user ? – contributor Jun 15 '19 at 08:13

1 Answers1

0

Try this.. When you do Auth::user it retrieves the current user model which will allow you to access user data

   public function storeComments(Request $request,Comment $body,$post_id){
    if($request->ajax()){
    $comment = new Comment;
    $comment->user_id =  Auth::user()->id;
    $comment->post_id = $post_id;
    $comment->body = Input::get('body');


    $comment->save();
   return response()->json( ['msg'=>$comment->body,'user_id' =>$comment->user_id, 'user_name' => Auth::user()->name, 'user_img' => Auth::user()->image] );       

    }
}
MekjkrhG
  • 211
  • 1
  • 8
  • that's cool, working ! but how could I append image.I've all images in folder `uploads/avatars`. what should I write in `src` exp: like how can I append it like `data.user_image` to `img` element $(".image_"+post_id).append("") – contributor Jun 15 '19 at 09:16
  • I've write something like this : `$(".image_"+post_id).append("")` but this loads just an icon type not the real image – contributor Jun 15 '19 at 09:42
  • it depends where you have stored the images.. Image can be stored in public folder i.e. in root/public, or in storage/app/public. – MekjkrhG Jun 15 '19 at 10:06
  • I've stored them in `uploads/avatars` but I got only an icon type , not real image of the user after writing above code, is that correct way ? – contributor Jun 15 '19 at 10:17
  • care to provide a screenshot? – MekjkrhG Jun 15 '19 at 10:18
  • check out the post, I've updated it with screenshot for you! @MekjkrhG – contributor Jun 15 '19 at 10:28
  • i found some useful link: https://stackoverflow.com/questions/18480550/how-to-load-all-the-images-from-one-of-my-folder-into-my-web-page-using-jquery – contributor Jun 15 '19 at 10:33
  • can you help me a little more, when the comment is done, I want to make the textarea empty, how can i do that , because right now comment is done successful but there is text in the textarea , I want to vanish that text when comment is done, any idea ? please share – contributor Jun 15 '19 at 10:36
  • glad you solved the image issue @contributor. as for the other question you better make a new post for that as its is going off topic now. – MekjkrhG Jun 15 '19 at 10:41