laravel后台新增或修改时控件为单选、多选

例一:新增或修改产品时,Product表里的district_id字段取District表里的name

控制器层:

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\District;
use App\Models\Product;
public function create()
{
$districts= District::where(‘status’, 1)->pluck(‘name’, ‘id’);
return view(‘admin.products.create’, compact(‘districts’));
}
public function edit($id){
$item= Product::find($id);
$districts= District::where(‘status’, 1)->pluck(‘name’, ‘id’);
return view(‘admin.products.edit’, compact(‘item’, ‘districts’));
}
view中的form:
<label for=”district_id” class=”col-sm-2 control-label”>服务地区:</label>
<divclass=”col-sm-3″>
{!! Form::select(‘district_id’, $districts,null, array(‘id’=>’district_id’,’class’ => ‘form-control’)) !!}
</div>
view中的list:
  <td>{{ $item->district->name}}</td>

962 total views, 2 views today

Revisions

There are no revisions for this post.

No comments yet.

发表评论