0

i want to get my current logged user details into my hidden input fields. so i write input values like this

<input type="hidden" name="name" v-model="comment.name">
<input type="hidden" name="avatar" v-model="comment.avatar">

or i want to get directly user informations into my vuejs objects

comment: {                  
          name: '',
          avatar: '',
          user_id: '',
          comment: '',
          status: 'Approved',
          created_at: ''
        },

how i get current logged user details into this object. i used this fetch method to get details

axios.get("api/userdetails").then(({ data }) => (this.users.fill(data)));

this is my controller

return auth('api')->user();
2
  • 1
    why are you doing this.users.fill(data)? Commented Dec 19, 2018 at 10:39
  • @ Boussadjra Brahim i want to show my current user data into my vuejs objects Commented Dec 19, 2018 at 10:55

3 Answers 3

2

How to get current authenticated user id using Vuejs and Laravel

You Should add to <meta name="user-id" content="{{ Auth::user()->id }}"> in

// app.blade.php or whatever you have maybe master.blade.php like below

    <head>
        <meta charset="UTF-8">
        @if (Auth::check()) 
          <meta name="user-id" content="{{ Auth::user()->id }}">
        @endif 
        <meta http-equiv="X-UA-Compatible" content="IE=edge" 
      ...
      ..

Then in the app.js or main.js add

 data: {
    comment: {
      name: '',
      cart: '',
      user_id: document.querySelector("meta[name='user-id']").getAttribute('content'),
    },

// Finally in your MyVueComponent.vue

<div class="col-sm-6">
    <label>First Name *</label>
    <input type="text" v-model="comment.name" class="form-control" required>
    <input type="hidden" v-model="comment.cart">
    <input type="hidden" v-model="comment.user_id">
</div>

hope will help someone

Sign up to request clarification or add additional context in comments.

Comments

0

Why not just pass the user variables straight into the blade template in the vuejs object?

{{ Auth::user()->id }} {{ Auth::user()->name }} {{ Auth::user()->avatar }}

Etc

1 Comment

how can i bind this with my objects
0

If i understood this well, this.users is list of all users you have in your, let's say, state? If this is true, then you have to recognize your currently logged user by some unique id he has, maybe you should use this.users.filter to find that user and compare id with currently logged user. Then just grab field you need to fill.

3 Comments

no i try to get current user details and bind it to users array objects
So currently logged user is something like Auth::user()->id etc, append it to this.users with spread operator : this.users = [...this.users, ...Auth::user()];
how can i write it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.