1

I have a vue component that will send a message and I want to send the logged in user name. I'm using laravel 5.5

methods:{
        sendMessage(){
            this.$emit('messagesent', {
                message:this.messageText,
                user: {
                    name : {{Auth()::user()->name}} //Here is where it should be
                }
            });
            this.messageText = '';
        }
    }

I already tried using

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

Both give errors. How can I retrieve the name?

Carlos F
  • 4,621
  • 3
  • 12
  • 19
  • Pass the username through as a prop and use it like you're using `this.messageText`. – Samsquanch Sep 12 '17 at 17:42
  • Possible duplicate of [Pass php variable to vue component instance](https://stackoverflow.com/questions/41520258/pass-php-variable-to-vue-component-instance) – Samsquanch Sep 12 '17 at 17:45

1 Answers1

2

Depend on your approach, this can be achieve in different ways
1. Pass in the username from the view <send-message user={{ $user->username }} ></send-message> then inside your vuejs component

export default {
    props: ['user'],
    data(){
        return {
           user: ''
        }
    },
    methods: {
        sendMessage(){
            var user = JSON.parse(this.user)
            //do you thing here
        }
    },
}

2. Bind the user information to window object on your page header

<script>
    window.user = {!! json_encode([
        'user' => $user->username,
    ]) !!};
</script>