尝试显示包含 Laravel 集体形式的视图 (edit.blade.php) 时,给出 'Invalid argument supplied for foreach()' - Laravel
When trying to show a view (edit.blade.php) containing a Laravel Collective form, gives 'Invalid argument supplied for foreach()' - Laravel
单击 show.blade.php 中的编辑按钮时,它应该显示 edit.blade.php,但显示 'Invalid argument supplied for foreach()'
其他一切正常...
知道我做错了什么吗?
整个错误消息: ErrorException
为 foreach() 提供的参数无效(视图:Real-estate/resources/views/properties/edit.blade.php)(127.0.0.1:8000/properties/8/edit)
foreach 循环 (in: vendor/laravelcollective/html/src/FormBuilder.php:656)
foreach ($list as $value => $display) {
$optionAttributes = $optionsAttributes[$value] ?? [];
$optgroupAttributes = $optgroupsAttributes[$value] ?? [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes);
}
观看次数
show.blade.php
<a href="/properties/{{ $property->id }}/edit" class="btn btn-light">Edit</a>
edit.blade.php
{!! Form::open(['action' => ['PropertiesController@update', $property->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
// a bunch of <div class="form-group"></div> go here
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Done', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
创建。blade.php
{!! Form::open(['action' => 'PropertiesController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
// a bunch of <div class="form-group"></div> go here
{{ Form::submit('Publish', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
控制器
public function store(Request $request)
{
$property = new Property;
$property->title = $request->input('title');
.
.
.
$property->save();
return redirect('/home');
}
public function update(Request $request, $id)
{
$property = Property::find($id);
$property->title = $request->input('title');
.
.
.
$property->save();
return redirect('/home');
}
dd($属性) 在 $属性->save() 之前;在上面的 public 函数 store() 中
App\Property {#1204 ▼
#table: "properties"
+primaryKey: "id"
#connection: null
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:10 [▼
"title" => "Nice House"
"reference_no" => "1234"
"published_date" => "2021-06-04"
"price" => "400k"
"property_type" => "example"
"area" => "example"
"city" => "Example city"
"description" => "example"
"images" => "noimage.jpg"
"user_id" => 2
]
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
}
你能检查一下 $list
的值是多少吗?
编辑:
$list
returns 一个字符串。
foreach($array as $key=>$value) {
// Do stuff
}
当使用上面的例子时,$array
是foreach使用的数组,$key
是每个数组项的键名,$value
是它对应的值。
在你的情况下,这是:
foreach ($list as $value => $display) {
...
}
当您尝试获取数组的键 ($value
) 和值 ($display
) 时,它收到为 提供的 无效参数foreach().
好的,那么问题就解决了。该错误是由错误配置的表单字段引起的:{{ Form::select }}
in create.blade.php。我漏了null
,所以应该是:
{{ Form::select('city', ['Example City 1' => 'Example City 1', 'Example City 2' => 'Example City 2' ], null, ['placeholder' => 'Select city...']) }}
单击 show.blade.php 中的编辑按钮时,它应该显示 edit.blade.php,但显示 'Invalid argument supplied for foreach()'
其他一切正常... 知道我做错了什么吗?
整个错误消息: ErrorException 为 foreach() 提供的参数无效(视图:Real-estate/resources/views/properties/edit.blade.php)(127.0.0.1:8000/properties/8/edit)
foreach 循环 (in: vendor/laravelcollective/html/src/FormBuilder.php:656)
foreach ($list as $value => $display) {
$optionAttributes = $optionsAttributes[$value] ?? [];
$optgroupAttributes = $optgroupsAttributes[$value] ?? [];
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes);
}
观看次数
show.blade.php
<a href="/properties/{{ $property->id }}/edit" class="btn btn-light">Edit</a>
edit.blade.php
{!! Form::open(['action' => ['PropertiesController@update', $property->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
// a bunch of <div class="form-group"></div> go here
{{ Form::hidden('_method', 'PUT') }}
{{ Form::submit('Done', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
创建。blade.php
{!! Form::open(['action' => 'PropertiesController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
// a bunch of <div class="form-group"></div> go here
{{ Form::submit('Publish', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
控制器
public function store(Request $request)
{
$property = new Property;
$property->title = $request->input('title');
.
.
.
$property->save();
return redirect('/home');
}
public function update(Request $request, $id)
{
$property = Property::find($id);
$property->title = $request->input('title');
.
.
.
$property->save();
return redirect('/home');
}
dd($属性) 在 $属性->save() 之前;在上面的 public 函数 store() 中
App\Property {#1204 ▼
#table: "properties"
+primaryKey: "id"
#connection: null
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:10 [▼
"title" => "Nice House"
"reference_no" => "1234"
"published_date" => "2021-06-04"
"price" => "400k"
"property_type" => "example"
"area" => "example"
"city" => "Example city"
"description" => "example"
"images" => "noimage.jpg"
"user_id" => 2
]
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
}
你能检查一下 $list
的值是多少吗?
编辑:
$list
returns 一个字符串。
foreach($array as $key=>$value) {
// Do stuff
}
当使用上面的例子时,$array
是foreach使用的数组,$key
是每个数组项的键名,$value
是它对应的值。
在你的情况下,这是:
foreach ($list as $value => $display) {
...
}
当您尝试获取数组的键 ($value
) 和值 ($display
) 时,它收到为 提供的 无效参数foreach().
好的,那么问题就解决了。该错误是由错误配置的表单字段引起的:{{ Form::select }}
in create.blade.php。我漏了null
,所以应该是:
{{ Form::select('city', ['Example City 1' => 'Example City 1', 'Example City 2' => 'Example City 2' ], null, ['placeholder' => 'Select city...']) }}