Laravel& Typeahead.js:检索类别名称但是将其ID提交给数据库?

时间:2015-09-27 06:23:49

标签: php mysql laravel laravel-5 typeahead.js

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

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

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

我该如何解决?

getSubreddits

中的

PostsController方法

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');

store()

中的整个PostsController.php方法
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() !!}

1 个答案:

答案 0 :(得分:0)

我得到了它的工作

<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);
});