4givN
4givN

Reputation: 3244

call route in laravel blade if exists only

I have an ajax function which will use an URL.

ajaxMyFunction('{{route('myroute.route1')}}', function (result) { //do stuff });

I want to call the route only if it exists in my dynamic route list because Laravel throws exception if myroute.route1 doesn't exist yet.

Is there a way to check route before render it inside blade, like when we check views ?

@if(View::exists('myroute.route1'))

I tried, Route::has('myroute.route1') but it doesn't work as well.

Thanx for your help guys ;)

Upvotes: 1

Views: 2311

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

As I explained in my Laravel best practices repo, you shouldn't mix JS and Blade syntax. To make it work, do something like this on the backend:

let route = {{ Route::has('myroute.route1') ? route('myroute.route1') : 'false' }}

And then in JS, you will be able to do something like this:

if (route) {
    ajaxMyFunction(route, ....);
}

Upvotes: 2

Related Questions