0

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?

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

1 Comment

@Aarohi Kulkarn - Please let me know above answer is your solution.

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.