任何人都可以帮我解决这个问题吗?我已经卡住了几天,错误 htmlspecialchars()希望参数1在Laravel 5.3中为字符串,数组为。
这是我的控制器
public function create()
{
$categories = DB::table('categories')->pluck('name', 'id');
foreach($categories as $category)
{
echo $category;
}
return view('admin.processor.create', compact('categories', $categories));
}
以下是我的观点(表格)
{!! Form::open(['url' => '/processor']) !!}
{!! csrf_field() !!}
<div class="form-group">
{!! Form::label('make', 'Manufacturer') !!}
{!! Form::select('make', ['Intel', 'AMD', 'Apple', 'Qualcomm', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('chipset', 'Chipset') !!}
{!! Form::text('chipset', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('type', 'Type') !!}
{!! Form::text('type', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('category', 'Category') !!}
{!! Form::select('categories', $categories, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add Processor', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
我只想达到与此相当的东西
<select class="form-control" name="category">
<?php
$stmt = $connect->prepare("SELECT * FROM categories");
$stmt->execute();
while($row = $stmt->fetch())
{
print "<option value='".$row['id']."'>".$row['name']."</option>";
}
?>
</select>
我的分类模型
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function processor()
{
return $this->hasMany('App\Processor', 'category_id');
}
}
我的处理器型号
use Illuminate\Database\Eloquent\Model;
class Processor extends Model
{
protected $fillable = ['make', 'chipset', 'type', 'category_id'];
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
}
答案 0 :(得分:2)
试试这个:
控制器
$categories = DB::table('categories')->pluck('name', 'id');
return view('admin.processor.create', compact('categories'));
查看
{!! Form::select('categories', $categories, null, ['class' => 'form-control']) !!}
答案 1 :(得分:2)
因此对于那些面临同样问题且仍然在Laravel 5.3中得到 htmlspecialchars()期望参数1为字符串,数组的人,我建议您使用普通的html表单而不是Laravel Collective的Form助手。
这就是我的所作所为 我的控制器
public function create()
{
$categories = Category::pluck('name', 'id');
return view('admin.processor.create', ['categories' => $categories]);
}
视图
<div class="form-group{{ $errors->has('category') ? ' has-error' : '' }}">
<label for="category">Category</label>
<select name="category" id="category" class="form-control" required autofocus>
@foreach($categories as $category)
<option value="{{ $category }}">{{ $category }}</option>
@endforeach
</select>
</div>
您现在应该可以显示创建页面了。希望它有所帮助。