Laravel 5.8 htmlspecialchars()期望参数1为字符串,数组给定为laravel 5.8

Laravel 5.8 htmlspecialchars()期望参数1为字符串,数组给定为laravel 5.8,laravel-5.8,Laravel 5.8,我想显示类别和paent_id。但我尝试了,但没有成功 category.blade.php <div class="form-group"> <label for="parent_id">Category</label> <select class="form-control" id="parent_id" name="parent_id"> <option value="">{{ $categorie

我想显示类别和paent_id。但我尝试了,但没有成功

category.blade.php

<div class="form-group">
    <label for="parent_id">Category</label>
    <select class="form-control" id="parent_id" name="parent_id">
        <option value="">{{ $categories }}</option>
    </select>
</div>
Category.php

protected $fillable = ['name', 'parent_id'];

public static function getCatList ()
{
    $array = array();
    $array[0] = 'Main Category';
    $category = self::with('getChild')->where('parent_id', 0)->get();
    foreach ($category as $key => $value) {
        $array[$value->id] = $value->name;
    }
    return $array;
}

public function getChild ()
{
    return $this->hasMany(Category::class, 'parent_id', 'id');
}
我看到这个错误

htmlspecialchars()要求参数1为字符串,数组给定(视图:C:\xampp\htdocs\new\shopping\resources\views\Admin\categories\create.blade.php)


首先,在
.blade
中不能使用没有循环的数组,因此
{{{$categories}
无效。使用循环:

@foreach($categories AS $category)
  <option value ...>
@endforeach
然后,在您的视图中,您可以在每个
选项中访问
$category->id
$category->name

@foreach($categories AS $category)
  <option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach

任何一种方法都可以。

您不能在
{{}
中使用数组,因此
{{$categories}
无效。你需要使用一个循环。解决方案是什么?“你需要使用一个循环。”有什么不清楚的吗?循环使用
$categories
变量,并为每个变量创建一个
。@foreach($categories as$category){{{$category->name}}@endforeach尝试获取非对象的属性时出错(视图:C:\xampp\htdocs\new\shopping\resources\views\Admin\categories\create.blade.php)然后,
$category
不是一个对象。您的代码表明这是
$value->name的结果
,所以可能只是
{{$category}
,而不是
{{{$category->name}
$categories = self::with('getChild')->where('parent_id', 0)->get();
foreach ($categories as $category) {
    $array[$category->id] = $category;
}
@foreach($categories AS $category)
  <option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
@foreach($categories AS $id => $name)
  <option value="{{ $id }}">{{ $name }}</option>
@endforeach