0

I am using a combination of laravel and angular.js . For angular I changed the interpolateProvider symbols to <% %>. For laravel i am using the standard {{ }} symbols.

Now to use an angular variable in my view and pass this to a function in laravel.

I tried this:

{{ route('getEdit', ['id' => <% loop.id %>]) }}

Where loop.id is from an ng-repeat from angular. And route is the routing function from laravel.

This gives an error :

syntax error, unexpected '<'

1 Answer 1

2

You can't pass javascript variable to PHP, you will need to use Ajax.

create an action in a controller called getRoute, post [/get-route/{route}]

PHP

public function getRoute($request, $route) {
    return route($route, $request->input('params'));
} 

and make a request to this route with your route name and a params object :

Jquery

$.post('{{ route('getRoute') }}', {params: [{id : loop.id}]}, function(data) {
    console.log(data); //should output the route with javascript variable loop.id
});

or using angular :

Angular

<script>
   var getRoute = '{{ route('getRoute') }}'; //Define your route inside a PHP file
</script>

$http.post(getRoute, {params: [{id : loop.id}]}).then(function(data) {
        console.log(data); //should output the route with javascript variable loop.id
});
Sign up to request clarification or add additional context in comments.

1 Comment

OP: "For angular I changed the interpolateProvider symbols to <% %>". I think they already did this.

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.