classes/Controllers/Base.php
public function autocomplete()
    {
        // Do nothing if the query is too short
        if (strlen(request('query')) < 1) {
            return Response::json();
        }

        // Get an instance so the title attributes can be found.  If none are found,
        // then there are no results, so bounce
        if (!$model = call_user_func([$this->model, 'first'])) {
            return Response::json($this->formatAutocompleteResponse([]));
        }

        // Get data matching the query
        $query = call_user_func([$this->model, 'titleContains'], request('query'))
            ->ordered()
            ->take(15); // Note, this is also enforced in the autocomplete.js

        // Don't return any rows already attached to the parent.  So make sure the
        // id is not already in the pivot table for the parent
        if ($this->isChildInManyToMany()) {

            // See if there is an exact match on what's been entered.  This is useful
            // for many to manys with tags because we want to know if the reason that
            // autocomplete returns no results on an exact match that is already
            // attached is because it already exists.  Otherwise, it would allow the
            // user to create the tag
            if ($this->parentRelation()
                ->titleContains(request('query'), true)
                ->count()) {
                return Response::json(['exists' => true]);
            }

            // Get the ids of already attached rows through the relationship function.
            // There are ways to do just in SQL but then we lose the ability for the
            // relationship function to apply conditions, like is done in polymoprhic
            // relationships.
            $siblings = $this->parentRelation()->get();
            if (count($siblings)) {
                $sibling_ids = [];
                foreach ($siblings as $sibling) {
                    $sibling_ids[] = $sibling->id;
                }

                // Add condition to query
                $model = new $this->model;
                $query = $query->whereNotIn($model->getQualifiedKeyName(), $sibling_ids);
            }
        }

        // Return result
        return Response::json($this->formatAutocompleteResponse($query->get()));
    }
public function autocomplete()
    {
        // Do nothing if the query is too short
        if (strlen(request('query')) < 1) {
            return Response::json();
        }

        // Get an instance so the title attributes can be found.  If none are found,
        // then there are no results, so bounce
        if (!$model = call_user_func([$this->model, 'first'])) {
            return Response::json($this->formatAutocompleteResponse([]));
        }

        // Get data matching the query
        $query = call_user_func([$this->model, 'titleContains'], request('query'))
            ->ordered()
            ->take(15); // Note, this is also enforced in the autocomplete.js

        // Don't return any rows already attached to the parent.  So make sure the
        // id is not already in the pivot table for the parent
        if ($this->isChildInManyToMany()) {

            // See if there is an exact match on what's been entered.  This is useful
            // for many to manys with tags because we want to know if the reason that
            // autocomplete returns no results on an exact match that is already
            // attached is because it already exists.  Otherwise, it would allow the
            // user to create the tag
            if ($this->parentRelation()
                ->titleContains(request('query'), true)
                ->count()) {
                return Response::json(['exists' => true]);
            }

            // Get the ids of already attached rows through the relationship function.
            // There are ways to do just in SQL but then we lose the ability for the
            // relationship function to apply conditions, like is done in polymoprhic
            // relationships.
            $siblings = $this->parentRelation()->get();
            if (count($siblings)) {
                $sibling_ids = [];
                foreach ($siblings as $sibling) {
                    $sibling_ids[] = $sibling->id;
                }

                // Add condition to query
                $model = new $this->model;
                $query = $query->whereNotIn($model->getQualifiedKeyName(), $sibling_ids);
            }
        }

        // Return result
        return Response::json($this->formatAutocompleteResponse($query->get()));
    }
public function autocomplete ( ) { // Do nothing if the query is too short if ( strlen ( request ( 'query' ) ) < 1 ) { return Response :: json ( ) ; } // Get an instance so the title attributes can be found.  If none are found, // then there are no results, so bounce if ( ! $ model = call_user_func ( [ $ this -> model , 'first' ] ) ) { return Response :: json ( $ this -> formatAutocompleteResponse ( [ ] ) ) ; } // Get data matching the query $ query = call_user_func ( [ $ this -> model , 'titleContains' ] , request ( 'query' ) ) -> ordered ( ) -> take ( 15 ) ; // Note, this is also enforced in the autocomplete.js // Don't return any rows already attached to the parent.  So make sure the // id is not already in the pivot table for the parent if ( $ this -> isChildInManyToMany ( ) ) { // See if there is an exact match on what's been entered.  This is useful // for many to manys with tags because we want to know if the reason that // autocomplete returns no results on an exact match that is already // attached is because it already exists.  Otherwise, it would allow the // user to create the tag if ( $ this -> parentRelation ( ) -> titleContains ( request ( 'query' ) , true ) -> count ( ) ) { return Response :: json ( [ 'exists' => true ] ) ; } // Get the ids of already attached rows through the relationship function. // There are ways to do just in SQL but then we lose the ability for the // relationship function to apply conditions, like is done in polymoprhic // relationships. $ siblings = $ this -> parentRelation ( ) -> get ( ) ; if ( count ( $ siblings ) ) { $ sibling_ids = [ ] ; foreach ( $ siblings as $ sibling ) { $ sibling_ids [ ] = $ sibling -> id ; } // Add condition to query $ model = new $ this -> model ; $ query = $ query -> whereNotIn ( $ model -> getQualifiedKeyName ( ) , $ sibling_ids ) ; } } // Return result return Response :: json ( $ this -> formatAutocompleteResponse ( $ query -> get ( ) ) ) ; }
List as JSON for autocomplete widgets

@return Illuminate\Http\Response JSON
List as JSON for autocomplete widgets