我正在尝试编辑用户,但是返回“未定义索引”。我正在使用此当前脚本来编辑ADDRESS信息并将其与用户相关联。
public function getIndexEditClient(Request $request, $id) {
$regions = DB::table("regions")->pluck("name","id");
$address = Address::where('id', $request->address_id)->with('region')->first();
$clients = $this->data->getEditClient($id);
//als gebruiker geen address heeft, creeer deze en koppel hem aan user
if (!isset($address)){
$data = $request->input();
$address = Address::create([
'region' => $data['region'],
'country' => $data['country'],
'city' => $data['city'],
'street_name' => $data['street_name'],
'house_number' => $data['house_number'],
'postcode' => $data['postcode']
]);
//create associate user with address
$clients->address()->associate($address);
$address = new Address;
$address->this->save();
其他情况很好。
这是我的编辑表单:
** <h3>Address information</h3>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Region</label>
<select id="region" name="region" class="form-control">
<option value="{{$address['region => id']}}" selected>{{$address['region => name']}}</option>
@foreach($regions as $key => $region)
<option value="{{$key}}"> {{$region}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Country</label>
<select name="country" id="country" class="form-control">
</select>
</div>
</div>
等等等
** 我添加地址的方式是否错误? 预先感谢。
答案 0 :(得分:0)
Laravel自动从请求中删除空值,因此,如果其中一个字段保留为空,则它将不在$data
数组中。
您应该在回发中添加验证,或者检查键[{1}}上是否存在密钥。
答案 1 :(得分:0)
尝试转储请求
dd($request->all());
如果所有值都来了,您可以使用
$address = Address::create($request->all());
答案 2 :(得分:0)
要保存一个数组,同时要保存一个对象,最好使用这种格式。因此,更改您要保存地址的方式
$data = new Address();
$data->region=$request->region;
$data->country=$request->country;
$data->city=$request->city;
...
$data->save();
$address = Address::create([
'region' => $data['region'],
'country' => $data['country'],
'city' => $data['city'],
'street_name' => $data['street_name'],
'house_number' => $data['house_number'],
'postcode' => $data['postcode']
]);