php - Laravel 和 Typeahead.js : retrieve category name but submit its ID to the database?

标签 php mysql laravel laravel-5 typeahead.js

我设法将 Typeahead.js 与我的 laravel 应用程序集成。但不幸的是,我需要提交它的 ID 而不是它的 name。我需要使用 Typeahead 检索类别的名称,但将其 ID 插入数据库,因为这就是我引用它的方式。

到目前为止,我收到了一个 sql 错误,因为它需要一个整数,但却得到了一个字符串(类别名称)。

我尝试执行 foreach 并将 $result->id 分配给 $data[] 但这根本不起作用。

我该如何解决?

PostsController 中的

getSubreddits 方法

public function getSubreddits($query) {
        $results = Subreddit::select('name')->where('name', 'LIKE', '%' . $query . '%')->get();
        return Response::json($results);
    }

JS

<script type="text/javascript">
        $(document).ready(function() {
            var subreddits = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                prefetch: 'http://localhost/reddit/public/data/subreddits',
                remote: {
                    url: 'http://localhost/reddit/public/data/subreddits/%QUERY',
                    wildcard: '%QUERY'
                }
            });

            $('#remote .typeahead').typeahead(null, {
                name: 'name',
                display: 'name',
                source: subreddits
            });
        });
</script>

路线

Route::get('data/subreddits', 'PostsController@getSubreddits');
Route::get('data/subreddits/{QUERY}', 'PostsController@getSubreddits');

PostsController.php 中的整个store() 方法

public function store(PostRequest $request)
    {
        if (Input::has('link')) {
            $input['link'] = Input::get('link');
            $info = Embed::create($input['link']);

            if ($info->image == null) {
                $embed_data = ['text' => $info->description];
            } else if ($info->description == null) {
                $embed_data = ['text' => ''];
            } else {
                $extension = pathinfo($info->image, PATHINFO_EXTENSION);

                $newName = public_path() . '/images/' . str_random(8) . ".{$extension}";

                if (File::exists($newName)) {
                    $imageToken = substr(sha1(mt_rand()), 0, 5);
                    $newName = public_path() . '/images/' . str_random(8) . '-' . $imageToken . ".{$extension}";
                }

                $image = Image::make($info->image)->fit(70, 70)->save($newName);
                $embed_data = ['text' => $info->description, 'image' => basename($newName)];
            }

            Auth::user()->posts()->create(array_merge($request->all(), $embed_data));

            return redirect('/articles');
        }
        Auth::user()->posts()->create($request->all());

        return redirect('/');
}

和形式

{!! Form::open(['url' => 'posts', 'method' => 'POST']) !!}
      <p>
         {!! Form::label('title', 'Title:') !!}
         {!! Form::text('title', null, ['class' => 'form-control', 'id' => 'title']) !!}
      </p>

      <p>
         {!! Form::label('link', 'Link:') !!}
         {!! Form::text('link', null, ['class' => 'form-control', 'id' => 'link']) !!}
      </p>

       <p>
          <div id="remote">
           <input class="form-control typeahead" type="text" placeholder="Choose a Subreddit" name="subreddit_id">
          </div>
       </p>

        <p>
           {!! Form::submit('Submit Post', ['id' => 'submit', 'class' => 'btn btn-primary']) !!}
         </p>

{!! Form::close() !!}

最佳答案

我成功了

<div id="remote">
    <input class="form-control typeahead" type="text" placeholder="Choose a Subreddit" name="subreddit_name">
    <input type="hidden" id="subreddit_id" name="subreddit_id" value="">  
</div>
...
$('#remote .typeahead').bind('typeahead:select', function(ev, suggestion) {
            $('#subreddit_id').val(suggestion.id);
});

关于php - Laravel 和 Typeahead.js : retrieve category name but submit its ID to the database?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32805185/

相关文章:

php - 在 Laravel 中找到交点的最佳方法

MySQL:在插入期间重新使用自动增量

laravel - Laravel:array_merge():参数2不是数组错误

php - 从数据库生成 CSS 类?

php - 无法在 Laravel 中获取 Auth::user->id

php - 性能方面的插值(直接插入字符串)VS拼接

javascript - 使用 PHP header 将文件保存在本地电脑中

php - 如何向sql server添加数据

mysql - 不同与分组依据

PHP MySQL 查询选择 DISTINCT 但按日期显示有限行