JpDevs JpDevs - 1 month ago 15
Javascript Question

Laravel URL Generator With Angular JS Varible

I am making an application with Angular JS as frond end and Laravel as back end.

I having a list of master data table view and a delete and edit options.

<a class="resedit btn btn-sm btn-default" href="{{URL::route('edit_repair_category',[[category.id]])}}/"><i class="icon-note"></i></a>
<a title="Delete" ng-click="deleteRow($event,category.id,'{{URL::route('delete_repair_category')}}',currentPage)" class=" btn btn-sm btn-delete"><i class="glyphicon glyphicon-trash"></i></a>


Above I am generating URL of Edit page with Laravel URL Generator, But I want to pass the ID of the master data to fetch the data from controller.

I am using [[ ]] for Angular JS bindings.

URL::route('edit_repair_category',[[category.id]])


Now I got the exception Use of undefined constant category - assumed 'category'

Routes.

Route::get('edit-repair-category/{id}', ['as' => 'edit_repair_category', 'uses' => 'RepairCategoryController@editRepairCategory']);


Is there any possibilities ?

Answer

The reason you're getting this error is because you're mixing javascript and PHP.

The PHP portion of you code is going to be processed on the server and then passed to the browser where the javascript is then going to be processed, so as far as PHP is concerned you are trying to use a constant.

Since category.id is just going on the end of the url you should be able to do something like:

<a class="resedit btn btn-sm btn-default" href="{{ URL::route('edit_repair_category', null) }}/@{{category.id}}"><i class="icon-note"></i></a>

null is being used here to prevent an error being thrown.

The @ before {{category.id}} just tells blade to treat this like a normal string and not to do anything with it (the @ symbol will be remove by blade).

Hope this helps!