0

I want to pass url parameter $id is dynamic value to Angularjs controller

My Angularjs:

$http.get('/todos/show/'+$id).success(function(data) {
  $scope.customers = data;
});

My Route in laravel

Route::get('todos/show/{id?}', function($id = null)
{
  $users = DB::table('customers')->where('id', $id)->get();
  return json_encode($users);
});

Can someone help me with these.

The Rock
  • 393
  • 3
  • 12
  • 29

1 Answers1

0

===Maybe I've got it wrong. See the correct answer at the end.===

First of all, you need setup your routerProvider: https://www.w3schools.com/angular/angular_routing.asp

After, you can set a route like this:

app.config(function($routeProvider) {
    $routeProvider.when("/todos/show/:id", {
        //your content here like example of w3schools
    })
});

Or

app.config(function($routeProvider) {
    $routeProvider.when("/todos/show/:id?", {
        //your content here like example of w3schools
    })
});

If id is an optional field.

Now, you need pass $routeParams to your controller, and you can get route data:

var myApp = angular.module('myApp',[]);

myApp.controller('Controller', ['$scope', '$routeParams', function($scope, $routeParams) {
  console.log($routeParams.id)
}]);

===Correct answer===

Use post! Angular:

$http.post('/todos/show', {id: $id}).success(function(data) {
  $scope.customers = data;
});

Laravel:

Route::post('todos/show', function(Request $request)
{
  $users = DB::table('customers')->where('id', $request->input('id'/*, 'optional default value'*/))->get();
  return json_encode($users);
});