laravel未定义属性中的关系:stdClass::error

laravel未定义属性中的关系:stdClass::error,laravel,Laravel,嗨我不熟悉laravel和雄辩的关系当我试图显示队形的类别名称时,我遇到了这个错误 未定义的属性:stdClass::$category(视图:E:\khibra platfrom\resources\views\formation\index.blade.php) 这是我的分类模型 class Category extends Model { function formations() { return $this->hasMany('Ap

我不熟悉laravel和雄辩的关系当我试图显示队形的类别名称时,我遇到了这个错误

未定义的属性:stdClass::$category(视图:E:\khibra platfrom\resources\views\formation\index.blade.php)

这是我的分类模型

class Category extends Model
{
    function formations()
        {
            return $this->hasMany('App\Formation');
        }

    protected $fillable =['name','description'];
} 
还有我的队形模型

class Formation extends Model
{
    function category()
        {
            return $this->belongsTo('App\Category',"category_id");
        }
}
我正试着像这样得到每个阵型的分类名称

 @foreach($formations as $formation)

          <tr>
            <th scope="row">{{$formation->id}}</th>
            <td>{{$formation->name}}</td>
            <td>{{$formation->price}}</td>
            <td>{{$formation->category->name}}</td>
            <td>{{$formation->durations}}</td>
            <td><div class="row">
          </tr>
    @endforeach
有人能帮忙吗


您使用的是
DB
facade非雄辩模型

$formations = DB::table('formations')->paginate('4');
将此更改为

Formation::with('category')->paginate(4); 
在模型编辑中也是这样

class Formation extends Model 
{
    public function category()
    {
        return $this->belongsTo('App\Category',"category_id");
    }
}
在刀片中,将代码更改为

<td>{{$formation->category->name ?? ''}}</td> 
{{$formation->category->name???'}

为了避免出现错误异常,当一个
没有
类别

可能重复时,我尝试了该问题中给出的解决方案,但对我无效?任何其他建议使用$formations=Formation::with('category);而不是$formations=DB::table('formations')->paginate('4');
<td>{{$formation->category->name ?? ''}}</td>