Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-编辑表单中的下拉选项_Php_Laravel_Html Select - Fatal编程技术网

Php Laravel-编辑表单中的下拉选项

Php Laravel-编辑表单中的下拉选项,php,laravel,html-select,Php,Laravel,Html Select,我正在拉威尔建立一个CRUD应用程序。在我的创建表单中,我让用户从下拉列表中选择一个选项,并将该选项的值发布到数据库 我现在正试图编辑该表单,但无法使用for-each循环动态获取值。但我可以手动操作 这是可行的,但不是动态的 <select class ="form-control" id="type" name="type"> <option value="">Select</option> <option {{ $Advert-&g

我正在拉威尔建立一个CRUD应用程序。在我的创建表单中,我让用户从下拉列表中选择一个选项,并将该选项的值发布到数据库

我现在正试图编辑该表单,但无法使用for-each循环动态获取值。但我可以手动操作

这是可行的,但不是动态的

<select class ="form-control" id="type" name="type">
    <option value="">Select</option>
    <option {{ $Advert->type == 'Apartment' ? 'selected':'' }}>Apartment</option>
    <option {{ $Advert->type == 'House' ? 'selected':'' }}>House</option>
    <option {{ $Advert->type == 'Studio' ? 'selected':'' }}>Studio</option>
    <option {{ $Advert->type == 'Flat' ? 'selected':'' }}>Flat</option>
</select>

假设您要编辑此
$Advert
对象,则必须将此对象连同从数据库获得的所有可用选项一起传递给视图。比如说

在控制器中:

$Advert = Advert::find($id)
$advert_types = // all the types that you'll get from your database
// maybe something like this
// $advert_types = DB::table('types')->get();
return view('yourview', compact('$Advert', 'advert_types'));
在您看来,您希望这样写:

<select class ="form-control" id="type" name="type">
  @foreach ($advert_types as $type)
    <option {{ $type->type == $Advert->type ? 'selected':'' }}>{{$type->type}}</option>
  @endforeach
</select>

@foreach($广告类型为$类型)
类型==$Advert->type?“所选“:”}>{{$type->type}
@endforeach

然后你就可以完成你的代码了。您无法在单个对象($Advert)中执行foreach操作,这就是为什么会出现错误的原因。

假设您要编辑此
$Advert
对象,则必须将此对象连同从数据库获得的所有可用选项一起传递给视图。比如说

<option value="male"@if($phone->gender=='male') selected='selected' @endif >Male</option>
在控制器中:

$Advert = Advert::find($id)
$advert_types = // all the types that you'll get from your database
// maybe something like this
// $advert_types = DB::table('types')->get();
return view('yourview', compact('$Advert', 'advert_types'));
在您看来,您希望这样写:

<select class ="form-control" id="type" name="type">
  @foreach ($advert_types as $type)
    <option {{ $type->type == $Advert->type ? 'selected':'' }}>{{$type->type}}</option>
  @endforeach
</select>

@foreach($广告类型为$类型)
类型==$Advert->type?“所选“:”}>{{$type->type}
@endforeach
然后你就可以完成你的代码了。您只是无法在单个对象($Advert)中执行foreach,这就是为什么会出现错误

gender=='male')selected='selected'@endif>male
<option value="male"@if($phone->gender=='male') selected='selected' @endif >Male</option>
我试过了。这是最好的解决办法。你可以试着享受

gender=='male')selected='selected'@endif>male
我试过了。这是最好的解决办法。您可以尝试在控制器中享受

$advert = Advert::find($id)
$advert_types = Advert::pluck('name', 'id');
return view('yourview', compact('$advert', 'advert_types'));
你的看法:

<select class ="form-control" id="type" name="type">
@foreach ($advert_types as $key => $value)
<option disable="true" value="{{ $key }}" {{ ($key == $advert) ? 'selected' : '' }}>{{$value}}
</option>
@endforeach
</select>

@foreach($key=>$value作为广告类型)
{{$value}}
@endforeach
在控制器中:

$advert = Advert::find($id)
$advert_types = Advert::pluck('name', 'id');
return view('yourview', compact('$advert', 'advert_types'));
你的看法:

<select class ="form-control" id="type" name="type">
@foreach ($advert_types as $key => $value)
<option disable="true" value="{{ $key }}" {{ ($key == $advert) ? 'selected' : '' }}>{{$value}}
</option>
@endforeach
</select>

@foreach($key=>$value作为广告类型)
{{$value}}
@endforeach

您的foreach循环没有任何意义。您正在以错误的方式使用$type变量。首先,请使用不同的名称更正此问题。输出
$Advert
变量后,结果如何<代码>变量转储($Advert);退出控制器中的code>?可能
type
属性在
$type
中不存在,我正在从控制器压缩$advert。我访问表单的其他部分,如$advert->title,作为变量。属性在同一张表中。@Gandhi先生foreach循环有什么问题?我让它工作了。我没有在编辑控制器中压缩类型,只是CreateYourForEach循环没有任何意义。您正在以错误的方式使用$type变量。首先,请使用不同的名称更正此问题。输出
$Advert
变量后,结果如何<代码>变量转储($Advert);退出控制器中的code>?可能
type
属性在
$type
中不存在,我正在从控制器压缩$advert。我访问表单的其他部分,如$advert->title,作为变量。属性在同一张表中。@Gandhi先生foreach循环有什么问题?我让它工作了。我没有在编辑控制器中压缩类型,只是创建了一个代码。虽然这段代码可以回答这个问题,但最好解释如何解决这个问题,并提供代码作为示例或参考。只有代码的答案可能会令人困惑,并且缺乏上下文。虽然此代码可能会回答问题,但最好解释如何解决问题,并提供代码作为示例或参考。只有代码的答案可能会令人困惑,并且缺乏上下文。