Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dev0ps's avatar

where add php array file in laravel

I am trying jquery typeahead

I add jquery

typeof $.typeahead === 'function' && $.typeahead({
    input: '.js-typeahead-hockey_v2',
    minLength: 1,
    maxItem: 8,
    maxItemPerGroup: 6,
    order: "asc",
    hint: true,
    searchOnFocus: true,
    blurOnTab: false,
    matcher: function (item, displayKey) {
        if (item.id === "BOS") {
            // Disable Boston for X reason
            item.disabled = true;
        }
        // Add all items matched items
        return true;
    },
    multiselect: {
        limit: 10,
        limitTemplate: 'You can\'t select more than 10 teams',
        matchOn: ["id"],
        cancelOnBackspace: true,
        data: function () {
 
            var deferred = $.Deferred();
 
            setTimeout(function () {
                deferred.resolve([{
                    "matchedKey": "name",
                    "name": "Canadiens",
                    "img": "canadiens",
                    "city": "Montreal",
                    "id": "MTL",
                    "conference": "Eastern",
                    "division": "Northeast",
                    "group": "teams"
                }]);
            }, 2000);
 
            deferred.always(function () {
                console.log('data loaded from promise');
            });
 
            return deferred;
 
        },
        callback: {
            onClick: function (node, item, event) {
                console.log(item);
                alert(item.name + ' Clicked!');
            },
            onCancel: function (node, item, event) {
                console.log(item);
                alert(item.name + ' Removed!');
            }
        }
    },
    templateValue: "{{name}}",
    display: ["name", "city"],
    emptyTemplate: 'no result for {{query}}',
    source: {
        teams: {
            url: "/jquerytypeahead/hockey_v2.json"
        }
    },
    callback: {
        onClick: function (node, a, item, event) {
            console.log(item.name + ' Added!')
        },
        onSubmit: function (node, form, items, event) {
            event.preventDefault();
 
            alert(JSON.stringify(items))
        }
    },
    debug: true
});

I add HTML

<form id="form-hockey_v2" name="form-hockey_v2">
    <div class="typeahead__container">
        <div class="typeahead__field">
 
            <span class="typeahead__query">
                <input class="js-typeahead-hockey_v2" name="hockey_v2[query]" type="search" placeholder="Search" autocomplete="off">
            </span>
            <span class="typeahead__button">
                <button type="submit">
                    <i class="typeahead__search-icon"></i>
                </button>
            </span>
 
        </div>
    </div>
</form>

but don't know where add PHP ?????????????????????

header('Content-Type" => application/json');
 
echo json_encode(array(
    array(
        "name"          => "Ducks",
        "img"           => "ducks",
        "city"          => "Anaheim",
        "id"            => "ANA",
        "conference"    => "Western",
        "division"      => "Pacific",
        "disabled"      => true
    ),
    array(
        "name"          => "Thrashers",
        "img"           => "thrashers",
        "city"          => "Atlanta",
        "id"            => "ATL",
        "conference"    => "Eastern",
        "division"      => "Southeast"
    ),
    array(
        "name"          => "Bruins",
        "img"           => "bruins",
        "city"          => "Boston",
        "id"            => "BOS",
        "conference"    => "Eastern",
        "division"      => "Northeast"
    ),
    array(
        "name"          => "Sabres",
        "img"           => "sabres",
        "city"          => "Buffalo",
        "id"            => "BUF",
        "conference"    => "Eastern",
        "division"      => "Northeast"
    ),
    array(
        "name"          => "Flames",
        "img"           => "flames",
        "city"          => "Calgary",
        "id"            => "CGY",
        "conference"    => "Western",
        "division"      => "Northwest"
    ),
    array(
        "name"          => "Hurricanes",
        "img"           => "hurricanes",
        "city"          => "Carolina",
        "id"            => "CAR",
        "conference"    => "Eastern",
        "division"      => "Southeast"
    ),
    array(
        "name"          => "Blackhawks",
        "img"           => "blackhawks",
        "city"          => "Chicago",
        "id"            => "CHI",
        "conference"    => "Western",
        "division"      => "Central",
        "disabled"      => true,
    ),
    array(
        "name"          => "Avalanche",
        "img"           => "avalanche",
        "city"          => "Colorado",
        "id"            => "COL",
        "conference"    => "Western",
        "division"      => "Northwest"
    ),
    array(
        "name"          => "Bluejackets",
        "img"           => "bluejackets",
        "city"          => "Columbus",
        "id"            => "CBJ",
        "conference"    => "Western",
        "division"      => "Central"
    ),
    array(
        "name"          => "Stars",
        "img"           => "stars",
        "city"          => "Dallas",
        "id"            => "DAL",
        "conference"    => "Western",
        "division"      => "Pacific"
    ),
    array(
        "name"          => "Red Wings",
        "img"           => "redwings",
        "city"          => "Detroit",
        "id"            => "DET",
        "conference"    => "Western",
        "division"      => "Central"
    ),
    array(
        "name"          => "Oilers",
        "img"           => "oilers",
        "city"          => "Edmonton",
        "id"            => "EDM",
        "conference"    => "Western",
        "division"      => "Northwest"
    ),
    array(
        "name"          => "Panthers",
        "img"           => "panthers",
        "city"          => "Florida",
        "id"            => "FLA",
        "conference"    => "Eastern",
        "division"      => "Southeast"
    ),
    array(
        "name"          => "Kings",
        "img"           => "kings",
        "city"          => "Los Angeles",
        "id"            => "LAK",
        "conference"    => "Western",
        "division"      => "Pacific"
    ),
    array(
        "name"          => "Wild",
        "img"           => "wild",
        "city"          => "Minnesota",
        "id"            => "MIN",
        "conference"    => "Western",
        "division"      => "Northwest"
    ),
    array(
        "name"          => "Canadiens",
        "img"           => "canadiens",
        "city"          => "Montreal",
        "id"            => "MTL",
        "conference"    => "Eastern",
        "division"      => "Northeast"
    ),
    array(
        "name"          => "Predators",
        "img"           => "predators",
        "city"          => "Nashville",
        "id"            => "NSH",
        "conference"    => "Western",
        "division"      => "Central"
    ),
    array(
        "name"          => "Devils",
        "img"           => "devils",
        "city"          => "New Jersey",
        "id"            => "NJD",
        "conference"    => "Eastern",
        "division"      => "Atlantic"
    ),
    array(
        "name"          => "Islanders",
        "img"           => "islanders",
        "city"          => "New York",
        "id"            => "NYI",
        "conference"    => "Eastern",
        "division"      => "Atlantic"
    ),
    array(
        "name"          => "Rangers",
        "img"           => "rangers",
        "city"          => "New York",
        "id"            => "NYR",
        "conference"    => "Eastern",
        "division"      => "Atlantic"
    ),
    array(
        "name"          => "Senators",
        "img"           => "senators",
        "city"          => "Ottawa",
        "id"            => "OTT",
        "conference"    => "Eastern",
        "division"      => "Northeast"
    ),
    array(
        "name"          => "Flyers",
        "img"           => "flyers",
        "city"          => "Philadelphia",
        "id"            => "PHI",
        "conference"    => "Eastern",
        "division"      => "Atlantic"
    ),
    array(
        "name"          => "Coyotes",
        "img"           => "coyotes",
        "city"          => "Phoenix",
        "id"            => "PHX",
        "conference"    => "Western",
        "division"      => "Pacific"
    ),
    array(
        "name"          => "Penguins",
        "img"           => "penguins",
        "city"          => "Pittsburgh",
        "id"            => "PIT",
        "conference"    => "Eastern",
        "division"      => "Atlantic"
    ),
    array(
        "name"          => "Sharks",
        "img"           => "sharks",
        "city"          => "San Jose",
        "id"            => "SJS",
        "conference"    => "Western",
        "division"      => "Pacific"
    ),
    array(
        "name"          => "Blues",
        "img"           => "blues",
        "city"          => "St. Louis",
        "id"            => "STL",
        "conference"    => "Western",
        "division"      => "Central"
    ),
    array(
        "name"          => "Lightning",
        "img"           => "lightning",
        "city"          => "Tampa Bay",
        "id"            => "TBL",
        "conference"    => "Eastern",
        "division"      => "Southeast"
    ),
    array(
        "name"          => "Maple Leafs",
        "img"           => "mapleleafs",
        "city"          => "Toronto",
        "id"            => "TOR",
        "conference"    => "Eastern",
        "division"      => "Northeast"
    ),
    array(
        "name"          => "Canucks",
        "img"           => "canucks",
        "city"          => "Vancouver",
        "id"            => "VAN",
        "conference"    => "Western",
        "division"      => "Northwest"
    ),
    array(
        "name"          => "Capitals",
        "img"           => "capitals",
        "city"          => "Washington",
        "id"            => "WSH",
        "conference"    => "Eastern",
        "division"      => "Southeast"
    ),
    array(
        "name"          => "Jets",
        "img"           => "jets",
        "city"          => "Winnipeg",
        "id"            => "WPG",
        "conference"    => "Eastern",
        "division"      => "Southeast"
    )
));
0 likes
17 replies
Dev0ps's avatar

getting

Use of undefined constant name - assumed 'name' (this will throw an Error in a future version of PHP)
rin4ik's avatar

here is your error

    templateValue: "{{name}}",

try with {{"name"}} or with {{$name}} if it is variable

1 like
rawilk's avatar

You could generate the array in your controller, send it to the view and then add it to your blade as a JavaScript variable like this:

<script>
    window.typeaheadOptions = {{ $typeaheadOptions }};
</script>
2 likes
Dev0ps's avatar

I don't understand your words. please be specific

my controller


    public function QueryShow(){

        header('Content-Type" => application/json');

        echo json_encode(array(
            array(
                "name"          => "Ducks",
                "img"           => "ducks",
                "city"          => "Anaheim",
                "id"            => "ANA",
                "conference"    => "Western",
                "division"      => "Pacific",
                "disabled"      => true
            ),
            array(
                "name"          => "Thrashers",
                "img"           => "thrashers",
                "city"          => "Atlanta",
                "id"            => "ATL",
                "conference"    => "Eastern",
                "division"      => "Southeast"
            ),
            array(
                "name"          => "Bruins",
                "img"           => "bruins",
                "city"          => "Boston",
                "id"            => "BOS",
                "conference"    => "Eastern",
                "division"      => "Northeast"
            ),
            array(
                "name"          => "Sabres",
                "img"           => "sabres",
                "city"          => "Buffalo",
                "id"            => "BUF",
                "conference"    => "Eastern",
                "division"      => "Northeast"
            ),
            array(
                "name"          => "Flames",
                "img"           => "flames",
                "city"          => "Calgary",
                "id"            => "CGY",
                "conference"    => "Western",
                "division"      => "Northwest"
            ),
            array(
                "name"          => "Hurricanes",
                "img"           => "hurricanes",
                "city"          => "Carolina",
                "id"            => "CAR",
                "conference"    => "Eastern",
                "division"      => "Southeast"
            ),
            array(
                "name"          => "Blackhawks",
                "img"           => "blackhawks",
                "city"          => "Chicago",
                "id"            => "CHI",
                "conference"    => "Western",
                "division"      => "Central",
                "disabled"      => true,
            ),
            array(
                "name"          => "Avalanche",
                "img"           => "avalanche",
                "city"          => "Colorado",
                "id"            => "COL",
                "conference"    => "Western",
                "division"      => "Northwest"
            ),
            array(
                "name"          => "Bluejackets",
                "img"           => "bluejackets",
                "city"          => "Columbus",
                "id"            => "CBJ",
                "conference"    => "Western",
                "division"      => "Central"
            ),
            array(
                "name"          => "Stars",
                "img"           => "stars",
                "city"          => "Dallas",
                "id"            => "DAL",
                "conference"    => "Western",
                "division"      => "Pacific"
            ),
            array(
                "name"          => "Red Wings",
                "img"           => "redwings",
                "city"          => "Detroit",
                "id"            => "DET",
                "conference"    => "Western",
                "division"      => "Central"
            ),
            array(
                "name"          => "Oilers",
                "img"           => "oilers",
                "city"          => "Edmonton",
                "id"            => "EDM",
                "conference"    => "Western",
                "division"      => "Northwest"
            ),
            array(
                "name"          => "Panthers",
                "img"           => "panthers",
                "city"          => "Florida",
                "id"            => "FLA",
                "conference"    => "Eastern",
                "division"      => "Southeast"
            ),
            array(
                "name"          => "Kings",
                "img"           => "kings",
                "city"          => "Los Angeles",
                "id"            => "LAK",
                "conference"    => "Western",
                "division"      => "Pacific"
            ),
            array(
                "name"          => "Wild",
                "img"           => "wild",
                "city"          => "Minnesota",
                "id"            => "MIN",
                "conference"    => "Western",
                "division"      => "Northwest"
            ),
            array(
                "name"          => "Canadiens",
                "img"           => "canadiens",
                "city"          => "Montreal",
                "id"            => "MTL",
                "conference"    => "Eastern",
                "division"      => "Northeast"
            ),
            array(
                "name"          => "Predators",
                "img"           => "predators",
                "city"          => "Nashville",
                "id"            => "NSH",
                "conference"    => "Western",
                "division"      => "Central"
            ),
            array(
                "name"          => "Devils",
                "img"           => "devils",
                "city"          => "New Jersey",
                "id"            => "NJD",
                "conference"    => "Eastern",
                "division"      => "Atlantic"
            ),
            array(
                "name"          => "Islanders",
                "img"           => "islanders",
                "city"          => "New York",
                "id"            => "NYI",
                "conference"    => "Eastern",
                "division"      => "Atlantic"
            ),
            array(
                "name"          => "Rangers",
                "img"           => "rangers",
                "city"          => "New York",
                "id"            => "NYR",
                "conference"    => "Eastern",
                "division"      => "Atlantic"
            ),
            array(
                "name"          => "Senators",
                "img"           => "senators",
                "city"          => "Ottawa",
                "id"            => "OTT",
                "conference"    => "Eastern",
                "division"      => "Northeast"
            ),
            array(
                "name"          => "Flyers",
                "img"           => "flyers",
                "city"          => "Philadelphia",
                "id"            => "PHI",
                "conference"    => "Eastern",
                "division"      => "Atlantic"
            ),
            array(
                "name"          => "Coyotes",
                "img"           => "coyotes",
                "city"          => "Phoenix",
                "id"            => "PHX",
                "conference"    => "Western",
                "division"      => "Pacific"
            ),
            array(
                "name"          => "Penguins",
                "img"           => "penguins",
                "city"          => "Pittsburgh",
                "id"            => "PIT",
                "conference"    => "Eastern",
                "division"      => "Atlantic"
            ),
            array(
                "name"          => "Sharks",
                "img"           => "sharks",
                "city"          => "San Jose",
                "id"            => "SJS",
                "conference"    => "Western",
                "division"      => "Pacific"
            ),
            array(
                "name"          => "Blues",
                "img"           => "blues",
                "city"          => "St. Louis",
                "id"            => "STL",
                "conference"    => "Western",
                "division"      => "Central"
            ),
            array(
                "name"          => "Lightning",
                "img"           => "lightning",
                "city"          => "Tampa Bay",
                "id"            => "TBL",
                "conference"    => "Eastern",
                "division"      => "Southeast"
            ),
            array(
                "name"          => "Maple Leafs",
                "img"           => "mapleleafs",
                "city"          => "Toronto",
                "id"            => "TOR",
                "conference"    => "Eastern",
                "division"      => "Northeast"
            ),
            array(
                "name"          => "Canucks",
                "img"           => "canucks",
                "city"          => "Vancouver",
                "id"            => "VAN",
                "conference"    => "Western",
                "division"      => "Northwest"
            ),
            array(
                "name"          => "Capitals",
                "img"           => "capitals",
                "city"          => "Washington",
                "id"            => "WSH",
                "conference"    => "Eastern",
                "division"      => "Southeast"
            ),
            array(
                "name"          => "Jets",
                "img"           => "jets",
                "city"          => "Winnipeg",
                "id"            => "WPG",
                "conference"    => "Eastern",
                "division"      => "Southeast"
            )
        ));

my route

Route::get('/jquerytypeahead/country_v2.json', 'AboutController@QueryShow')->name('home');
Dev0ps's avatar

my status.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <meta name="csrf-token" content="{{ csrf_token() }}" />
</head>
<body>


<script type="text/javascript" src="{{ asset('js/jquery.min.js') }}"></script>
<link href="{{ asset('css/jquery.typeahead.min.css') }}" rel="stylesheet" type="text/css" >


    <form id="form-country_v1" name="form-country_v1">
        <div class="typeahead__container">
            <div class="typeahead__field">

            <span class="typeahead__query">
                <input class="js-typeahead-country_v1" name="country_v1[query]" type="search" placeholder="Search" autocomplete="off">
            </span>
                <span class="typeahead__button">
                <button id="aa" type="submit">
                    <i class="typeahead__search-icon"></i>
                </button>
            </span>

            </div>
        </div>
    </form>

<form id="form-hockey_v2" name="form-hockey_v2">
    <div class="typeahead__container">
        <div class="typeahead__field">

            <span class="typeahead__query">
                <input class="js-typeahead-hockey_v2" name="hockey_v2[query]" type="search" placeholder="Search" autocomplete="off">
            </span>
            <span class="typeahead__button">
                <button type="submit">
                    <i class="typeahead__search-icon"></i>
                </button>
            </span>

        </div>
    </div>
</form>



<script>
    typeof $.typeahead === 'function' && $.typeahead({
        input: '.js-typeahead-hockey_v2',
        minLength: 1,
        maxItem: 8,
        maxItemPerGroup: 6,
        order: "asc",
        hint: true,
        searchOnFocus: true,
        blurOnTab: false,
        matcher: function (item, displayKey) {
            if (item.id === "BOS") {
                // Disable Boston for X reason
                item.disabled = true;
            }
            // Add all items matched items
            return true;
        },
        multiselect: {
            limit: 10,
            limitTemplate: 'You can\'t select more than 10 teams',
            matchOn: ["id"],
            cancelOnBackspace: true,
            data: function () {

                var deferred = $.Deferred();

                setTimeout(function () {
                    deferred.resolve([{
                        "matchedKey": "$name",
                        "name": "Canadiens",
                        "img": "canadiens",
                        "city": "Montreal",
                        "id": "MTL",
                        "conference": "Eastern",
                        "division": "Northeast",
                        "group": "teams"
                    }]);
                }, 2000);

                deferred.always(function () {
                    console.log('data loaded from promise');
                });

                return deferred;

            },
            callback: {
                onClick: function (node, item, event) {
                    console.log(item);
                    alert(item.name + ' Clicked!');
                },
                onCancel: function (node, item, event) {
                    console.log(item);
                    alert(item.name + ' Removed!');
                }
            }
        },
        templateValue: "{{name}}",
        display: ["name", "city"],
        emptyTemplate: 'no result for {{query}}',
        source: {
            teams: {
                url: "/123"
            }
        },
        callback: {
            onClick: function (node, a, item, event) {
                console.log(item.name + ' Added!')
            },
            onSubmit: function (node, form, items, event) {
                event.preventDefault();

                alert(JSON.stringify(items))
            }
        },
        debug: true
    });

</script>

</body>
</html>
rin4ik's avatar

try to remove this line and see if issue related with it

   display: ["name", "city"],
1 like
Dev0ps's avatar

when i remove these 3 lines

 templateValue: "{{name}}",
        display: ["name", "city"],
        emptyTemplate: 'no result for {{query}}',

page opens. but brother we need to integrate this big array to this jquery

Dev0ps's avatar

when i try to fetch data from controller

jquery.typeahead.min.js:10 Uncaught TypeError: Cannot read property 'toString' of undefined
    at a.getTemplateValue (jquery.typeahead.min.js:10)
    at a.populateMultiselectData (jquery.typeahead.min.js:11)
    at Object.<anonymous> (jquery.typeahead.min.js:11)
    at Object.<anonymous> (jquery.min.js:2)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at Object.e.(:8000/anonymous function) [as resolve] (http://127.0.0.1:8000/js/jquery.min.js:2:28929)
    at (index):519
rin4ik's avatar

problem I think still remains with this

 templateValue: "{{name}}",

what is name? why this with curly braces? maybe it is item.name? or what?

1 like
Dev0ps's avatar

yes you are right

name is the variable of this big array

echo json_encode(array(
            array(
                "name"          => "Ducks",
                "img"           => "ducks",
                "city"          => "Anaheim",
                "id"            => "ANA",
                "conference"    => "Western",
                "division"      => "Pacific",
                "disabled"      => true
            ),
            array(
                "name"          => "Thrashers",
                "img"           => "thrashers",
                "city"          => "Atlanta",
                "id"            => "ATL",
                "conference"    => "Eastern",
                "division"      => "Southeast"
            ),
....
....
1 like
Dev0ps's avatar

when i select somebodies name. it add "name" in place of username

Please or to participate in this conversation.