Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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 - Fatal编程技术网

Php Laravel无法获取类别的父级

Php Laravel无法获取类别的父级,php,laravel,Php,Laravel,我正在尝试创建类别,我想显示类别名称及其父类别,但我一直收到以下错误消息: 正在尝试获取非对象的属性“name” 这是我的模型: public$timestamps=false; public$primaryKey='id'; 公共职能类别{ 返回$this->hasManycategory::class; } 公共功能儿童分类{ 返回$this->hasManyCategory::class->with'categories'; } 公共职能家长{ 返回$this->belongsToCate

我正在尝试创建类别,我想显示类别名称及其父类别,但我一直收到以下错误消息:

正在尝试获取非对象的属性“name”

这是我的模型:

public$timestamps=false; public$primaryKey='id'; 公共职能类别{ 返回$this->hasManycategory::class; } 公共功能儿童分类{ 返回$this->hasManyCategory::class->with'categories'; } 公共职能家长{ 返回$this->belongsToCategory::class,'category_id'; } 这是我的观点:

@foreach$类别作为$类别 {{$category->id} {{$category->name} {{$category->parent->name} @endforeach
问题出在类别为父类别的类别中,类别id为空

@foreach($categories as $category )
    <tr>
        <td>{{$category->id}}</td>
        <td>{{$category->name}}</td>
        <td>{{$category->parent != null ? $category->parent->name : ''}}</td>
    </tr>
@endforeach

您的某些类别没有父类别。问题在于类别id为空的父类别将父名称行更改为{{{$category->parent->name???}您可以在更新的答案中使用whereNotNull!
$categories = Category::whereNotNull('category_id');