1

I am working on a laravel web application , recently i embedded angularjs for searching data from database. But for now i just used an array to search.In my AngularJs Module i point to search function as $scope.url = 'Students@search'; but this is not working for me. I want to know that how to tell AngularJS $scope.url to point to this search function in my Controller so that user can search data easily. I have search function in Students Controller:

 public function search(){
            $data = file_get_contents("php://input");

            $objData = json_decode($data);

            // Static array for this demo
            $values = array('php', 'web', 'angularjs', 'js');

            // Check if the keywords are in our array
            if(in_array($objData->data, $values)) {
                echo 'I have found what you\'re looking for!';
            }
            else {
                echo 'Sorry, no match!';
            }
  }

MyApp.js File :

function SearchCtrl($scope, $http) {
    $scope.url = 'Students@search'; // The url of our search

    // The function that will be executed on button click (ng-click="search()")
    $scope.search = function() {

        // Create the http post request
        // the data holds the keywords
        // The request is a JSON request.
        $http.post($scope.url, { "data" : $scope.keywords}).
        success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
            $scope.result = data; // Show result from server in our <pre></pre> element
        })
        .
        error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;         
        });
    };
}

Angular Search View:

<div ng-controller="SearchCtrl">
                                    <form class="well form-search">
                                        <label>Search:</label>
                                        <input type="text" ng-model="keywords" class="input-medium search-query" placeholder="Keywords...">
                                        <button type="submit" class="btn" ng-click="search()">Search</button>
                                        <p class="help-block">Try for example: "php" or "angularjs" or "asdfg"</p>      
                                    </form>
                                    <pre ng-model="result">
                                    @{{result}}
                                    </pre>
</div>

1 Answer 1

0

You may create a route first. For example-

Route::get("/search", ["as" => "studentSearch", "uses" => "Students@search"]);

then you can set it like

 $scope.url = '{{ route ("studentSearch") }}';

Or if its not a blade template then you have to manually set the url. Like

 $scope.url = '/search';
Sign up to request clarification or add additional context in comments.

3 Comments

Route [studentSearch] not defined.
Check my edit. route will be Route::get("/search", ["as" => "studentSearch", "uses" => "Students@search"]);
bro not working when i click submit it does nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.