Search code examples
laravelvue.jslaravel-controller

how to access data ($count) from laravel controller to vue component


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!


Solution

  • 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>