Search code examples
laravelvue.jslaravel-bladevuex

Passing Laravel routes to vue components


I have an index.blade.php and I included vue codes in this view. In a normal situation we'll pass the laravel route like so :

<a href="{{route('some-route')}}"> Link </a>

Imagine I have a master vue component inside this index.blade.php and I'm doing this right now :

@section('body')
    <router-view
        {{-- Props --}}
        prop1	 = "{{route('route1')}}"
        prop2	 = "{{route('route2')}}"
        prop3	 = "{{route('route3')}}"
        prop4    = "{{route('route4')}}"
        prop5    = "{{route('route5')}}"
        prop6    = "{{route('route6')}}"
        prop7    = "{{route('route7')}}"
        ...
    </router-view>
@endsection

I have so many routes that I want to use in my vue components and Im doing something like this ...

Sometimes it gets frustrating to write all of these routes and using props and etc.

Is there a better way to do this ?

Thanks :)


Solution

  • You can use Route::getRoutes() this will return RouteCollection this way you can do

    $routeCollection = Route::getRoutes();
    
    @section('body')
    <router-view
        {{-- Props --}}
    @foreach(Route::getRoutes() as $key => $route)
        prop{{ $key }} = "{{ $route->getPath() }}"
    
    </router-view>
    @endsection