【发布时间】:2018-06-30 13:29:11
【问题描述】:
所以我有这个 CRUD,我使用相同的表单来创建和编辑条目。 我需要几种形式的选择,在创建(该特定字段还没有数据)时,我选择显示占位符,但是在编辑时,我选择显示存储在数据库中的特定 id 字段的任何内容。所以我有: 控制器:
...
public function create()
{
$house = houses::pluck('name', 'id');
//$thisclient = null;
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
return view('prospects.create', compact('house', 'clients', 'reps'));
}
...
public function edit($id)
{
$house = houses::pluck('name', 'id');
//$thisclient = user::whereId($id)->first();
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
$prospect = Prospect::findOrFail($id);
return view('prospects.edit', compact('prospect', 'house', 'clients', 'reps'));
}
和我的视图形式: 为创造工作:
{!!Form::select('client_id', $clients, null, ['class' => 'form-control', 'placeholder' => 'Please Select'] ) !!}
编辑工作:
{!! Form::select('client_id', $clients, $prospect->client_id, ['class' => 'form-control'] ) !!}
我在这里遇到了2个问题,如果我有null作为我的选择字段,它不会将选择的数据带到edit,如果我有$prospect->client_id,它将在@987654327上返回错误@因为还没有数据。
我试图通过在控制器上创建一个变量 $thishouse 并将其传递给查看返回 view('prospects.create', compact('house', 'thisclient','clients', 'reps')); 和查看 Form::select('client_id', $clients, $thisclient, ['class' => 'form-control'] ) !!} 来解决这个问题,但是当有多个表单选择时似乎有点脏......
第二个麻烦是如果我在编辑上留下一个占位符,它将显示占位符,而不是 $prospect->client_id 本身。
实现所有这些并使用相同的表单进行创建和编辑的最佳和最简单的方法是什么? 谢谢
【问题讨论】:
标签: php forms laravel laravel-5.5 laravel-blade