[英]Auto Select option form Laravel
i want to make my edit is auto select option but when i try is doesnt work. 我想让我的编辑是自动选择选项,但是当我尝试不起作用时。 i need my form edit is auto select the data if i click data with id_cms_companies "1" from my table location is auto select and show in the first select option. 我需要我的表单编辑是自动选择数据,如果我从表位置单击id_cms_companies为“ 1”的数据是自动选择并显示在第一个选择选项中。 and its show the name of table cms_companies here my code : 并在此处显示我的代码表cms_companies的名称:
<select class='form-control' name='id_cms_companies'>
<option>- Companies -</option>
<?php
$category = DB::table("cms_companies")->get();
foreach($category as $cat)
{
if($cat->name == $row->id_cms_companies)
{
echo "<option value='$cat->name' selected>$cat->name</option>";
}
else
{
echo "<option value='$cat->name'>$cat->name</option>";
}
}
?>
</select>
and here my controller : 这是我的控制器:
public function getEdit($id) {
$data['row'] = locations::find($id);
return view('locations_form',$data);
}
public function postEditSave($id) {
$simpan= array();
$simpan['name']=Request::input('name');
$simpan['id_cms_companies']=Request::input('id_cms_companies');
DB::table('locations')->where('id', $id)->update($simpan);
Session::flash('edit', 'Data berhasil di Edit');
return Redirect::to('locations');
}
here my table : 这是我的桌子:
and here for my 还有我的
You should compare two IDs, no ID with Name 您应该比较两个ID,没有ID与Name
if($cat->name == $row->id_cms_companies) //wrong
if($cat->id == $row->id_cms_companies) //correct
You could use laravel html , it will do it automatically 您可以使用laravel html ,它将自动完成
So you could do something like this: 因此,您可以执行以下操作:
$category = DB::table("cms_companies")->get()->pluck('name', 'id');
echo Form::select('id_cms_companies', $category, $row->id_cms_companies, ['class' => 'form-control']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.