类别型号
\nclass Category extends Model\n{\nuse HasFactory;\n\nprotected $fillable= ['name','number'];\n\n public function news(){\n\n return $this->hasMany(News::class);\n}\nRun Code Online (Sandbox Code Playgroud)\n新闻模型
\nclass News extends Model\n{\nuse HasFactory;\n\nprotected $fillable= ['cat_id','title','photo','description','author_id','status'];\n\n public function category(){\n return $this->belongsTo(Category::class);\n }\n\n public function author(){\n return $this->belongsTo(Author::class);\n }\nRun Code Online (Sandbox Code Playgroud)\n作者模型
\nclass Author extends Model\n{\nuse HasFactory;\n\nprotected $fillable= ['name','status'];\n\npublic function news(){\n return $this->hasMany(News::class);\n}\nRun Code Online (Sandbox Code Playgroud)\n3个模型之间存在关系。我想在 中显示类别名称而不是category_id news-list.blade.php。我收到此错误。
这是我的控制器功能
\n public function news_index(){\n $news= News::with('category')->get();\n return view('News.list',compact('news'));\n}\nRun Code Online (Sandbox Code Playgroud)\n这是我的刀片页面。$new->category->name当我输入而不是 时出现错误cat_id。
@foreach($news as $new)\n <tr>\n <th scope="row">{{$loop->iteration}}</th>\n <td> …Run Code Online (Sandbox Code Playgroud)