【发布时间】:2014-05-19 18:47:27
【问题描述】:
我的表单中有一个自动完成字段,可让我选择联系人姓名并将其与列表关联。
然而,在编辑页面上,它将显示联系人 ID,而不是他的名字和姓氏。
我该怎么做?
这是ListingsController.php中的getEdit()方法
public function getEdit($id)
{
// get the nerd
$listings = Listing::find($id);
$listings->images = Image::where('listing_id', $id)->get();
$ImagesFileName = "";
$listings->imagesFiles = "";
if ($listings->images) {
foreach ($listings->images as $CurImage) {
$ImagesFileName[] = $CurImage['image_name'];
}
$listings->imagesFiles = implode(",", $ImagesFileName) ;
}
$query = DB::table('contacts')->get();
foreach ($query as $firstname)
{
$name[] = $firstname->firstname." ".$firstname->lastname;
}
$fullname = json_encode($name);
// show the edit form and pass the nerd
$this->layout->content = View::make('listings/edit')
->with('listings', $listings)
->with('getClientsByLetters', $fullname);
}
这是ListingsController.php中的action_getClientsByLetters()
public function action_getClientsByLetters() {
$term = Input::get('query');
$data = array();
$query = DB::query("
SELECT * FROM contacts
WHERE MATCH (contact)
AGAINST('+".$term."*' IN BOOLEAN MODE)
");
foreach ($query as $results => $contact) {
$data[] = array(
'id' => $contact->id,
'value' => $contact->firstname
);
}
return json_encode($data);
}
这是views/listings/edit.blade.php中的字段
<p>{{ Form::text('contact_id', null, array('class'=>'form-control', 'placeholder'=>'Contact Name', 'id'=>'contact', 'onblur'=>'test()')) }}</p>
这是同一视图上的 javascript 代码
$(function() {
var availableTags = <?php echo $getClientsByLetters; ?>;
$( "#contact" ).autocomplete({
source: availableTags,
minLength: 2,
});
});
【问题讨论】:
标签: php mysql laravel laravel-4 blade