0
模型我有具有功能如下一个Post模型上的方法的属性:访问返回在Laravel
namespace App;
use App\Category;
class Post extends Model
{
public function category()
{
return $this->hasOne(Category::class);
}
}
我的分类模型看起来像这样
namespace App;
use App\Post;
class Category extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
在我查看,我想访问名称字段的类别为每个职位。
我认为我将有机会获得这种模式,因此可以通过在我的刀片文件这样得到它:
{{ $post->category()->name }}
$职位是正确的,我有机会到其他属性,但此引发错误:
Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$name
任何想法?
,这将引发“试图获得非对象的财产”错误。 – helenkitt
'Post'有一个'category_id'字段?或者'Category'有一个'post_id'字段? – Laerte
该字段仅称为类别。但已经通过,并将其更改为category_id我收到错误“SQLSTATE [42S22]:未找到列:1054未知列'categories.post_id'” – helenkitt