我正在尝试保存包含动态下拉列表的动态表单内容。我已经使用Ajax和Java脚本来实现动态下拉菜单,并使用get()函数来从数据库检索数据。
我尝试使用first()代替get()。但这会导致另一个错误“试图获取非对象的属性”。还会导致禁用动态下拉选项。
JobsController.php
function fetch(Request $request)
{
$select = $request->get('select');
$value = $request->get('value');
$dependent = $request->get('dependent');
$data = DB::table('specializations')->where($select, $value)**->first();**
$output = '<option value="">--Select Specialization--</option>';
foreach($data as $row)
{
$output .= '<option value="'.$row->id.'">'.$row->title.'</option>';
}
echo $output;
public function store(Request $request)
{
$this->validate($request,[
'expertise'=>'required',
'specialization'=>'required'
]);
$job = new JobsController;
$job->expertise_id = $request->input('expertise');
$job->specialization_id = $request->input('specialization');
$job->save();
return redirect('job.create_job')->with('success','Job Added');
}
create_job.blade.php
{!! Form::open(['action'=>'JobsController@store','method'=>'POST'])!!}
<div class="form-group">
<label for="">Expertise</label>
<select name="expertise" id="expertise" class="form-control input dynamic" data-dependent="specialization">
<option value="" disabled="true" selected="true">--Select your tradesmen--</option>
@foreach ($expertises as $expertise)
<option value="{{$expertise->id}}">{{$expertise->title}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Specialization</label>
<select name="specialization" id="specialization" class="form-control input">
<option value="">--Select Specialization--</option>
</select>
</div>
{{ csrf_field() }}
{{Form::submit('Submit',['class'=>'btn btn-primary'])}}
{!! Form::close()!!}
我应该能够保存数据。但是save()方法不起作用,并显示错误“方法[保存]不存在”错误。