获取json数据并使用Laravel转换为数组

时间:2017-04-15 19:32:52

标签: jquery json ajax laravel typeahead.js

我正在尝试使用typeahead。如何获取json数据并转换为状态的jquery数组?我已阅读文档,但没有运气。 (我用,1.9.1 jquery,Laravel 5.1)

以下是有效但状态数组是静态的。我想从MySql数据库异步中获取数据。怎么做?

jquery的:

var substringMatcher = function(strs) {
  return function findMatches(q, cb) {
    var matches, substringRegex;

    // an array that will be populated with substring matches
    matches = [];

    // regex used to determine if a string contains the substring `q`
    substrRegex = new RegExp(q, 'i');

    // iterate through the pool of strings and for any string that
    // contains the substring `q`, add it to the `matches` array
    $.each(strs, function(i, str) {
      if (substrRegex.test(str)) {
        matches.push(str);
      }
    });

    cb(matches);
  };
};

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

$('#the-basics .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  source: substringMatcher(states)
});

我的路线是:

Route::get('search', array('as' => 'searchlive', 'uses' => 'HistoryController@searchlive'));

我的控制器:

public function searchlive(Request $request)
    {
        $search = $request->thesearch;
        $games = Game::where('id', 'LIKE', '%' . $search . '%')
            ->orwhere('name', 'LIKE', '%' . $search . '%')
            ->orwhere('title', 'LIKE', '%' . $search . '%')->take(5)->get();

        return Response()->json($games);
    }

1 个答案:

答案 0 :(得分:1)

如果您只需要数组到您的预先类型jquery代码中,为什么需要获取json数据。只需将数组放入控制器并将其传递给您的预先输入代码,例如:

public function searchlive(Request $request)
{
    $search = $request->thesearch;
    $games = Game::where('id', 'LIKE', '%' . $search . '%')
        ->orwhere('name', 'LIKE', '%' . $search . '%')
        ->orwhere('title', 'LIKE', '%' . $search . '%')->take(5)->get();

       $gamesarray = $games->toArray();
       return view('yourbladefilename', compact(['gamesarray']))
}

在您的刀片文件中替换:

    var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

var states = {!! $gamesarray !!}

希望它能帮助......如有任何问题,请发表评论..