gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:35:03 24 4
gpt4 key购买 nike

我设法将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com