1

I want to access a variable $count from my controller and access it to a vue component (frontend). currently have this code on my controller:

public function index()
{
    $count = User::where('created_at', Carbon::today())->count();
    return view('user.index', compact('count'));
}

i am quite new to laravel and vuejs so please help me out. thank you!

2
  • define component in user/index.blade.php file. $count is available in blade file so passed as prop in component definition. Commented Mar 7, 2022 at 5:50
  • 1
    @JinalSomaiya hi! thank you for taking the time to answer :) Commented Mar 7, 2022 at 8:07

2 Answers 2

5

Send count as a prop from blade to component

<your-component-name :count="{{ $count }}"></your-component-name>

And from your component, receive that prop

<template>
    <div>
        Count is {{ count }}
    </div>
</template>

<script>
export default {
    props: {
        count: {
            type: Number
        }
    }
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

hi! thank you for taking the time to answer :)
Not at all, I hope it worked
0

To pass down data to your components you can use props. Find more info about props over here. This is also a good source for defining those props.

You can do something like:

<example :userId="{{ Auth::user()->id }}"></example>

OR

<example v-bind:userId="{{ Auth::user()->id }}"></example>

And then in your Example.vue file you have to define your prop. Then you can access it by this.userId.

Like :

<script>
  export default {
    props: ['userId'],
    mounted () {
      // Do something useful with the data in the template
      console.dir(this.userId)
    }
  }
</script>

the above question was answered in below question Pass data from blade to vue component

1 Comment

hi! thank you for taking the time to answer :)

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.