Reputation: 427
I just migrated to Laravel from Django. I am not sure how to send variable number of parameters through my AJAX request from Angularjs to a GET method in Laravel.
I have a certain number of filters to a list and I only want to send those that are not undefined, along with their key, so that I can directly do this:
Group::where($filters)->get();
$filters is the array I intent to send through Angularjs, which will be something like
['group_id'=>101,'Country'=>'India']
Or whatever syntax it requires.
Also, how exactly do I specify this in my get route in route.php?
EDIT:
I know I can always go with the optional parameters and if else, but there has got to be a better way, isn't there?
Upvotes: 0
Views: 864
Reputation: 1609
1). Angular $http get request.
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Today's welcome message is:</p>
<h1>{{myWelcome}}</h1>
</div>
<p>The $http service requests a page on the server, and the response is set as the value of the "myWelcome" variable.</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http({
method : "GET",
url : "{! URL::to('/project/project-data') !}"
}).then(function mySucces(response) {
$scope.myWelcome = response.data;
}, function myError(response) {
$scope.myWelcome = response.statusText;
});
});
</script>
</body>
</html>
2). Create Controller for ajax request.
namespace App\Http\Controllers;
class ProjectController extends BaseController {
public function ProjectData(){
echo "your project data placed here";
exit()
}
}
3). Define route url in routes.php
Route::get('project/project-data', 'ProjectController@ProjectData');
Upvotes: 1