//category model
class Category extends Model
{
protected $table="category";
protected $fillable = [
'CategoryName','Description','status'
];
}
//Blog model
class Blog extends Model
{
protected $table="blog";
protected $fillable = [
'BlogTitle','Description','status','filepath','category','comments','rfilepath'
];
}
//controller
$blogs = Blog::Where('status','=','active')->get();
return View::make("categories.viewblog")->with("blogs", $blogs);我只能在视图部分获得活跃的博客,但如果我的类别是非活动的,则不应该在视图部分查看类别,反之亦然。我认为建立关系是正确的!
发布于 2016-12-11 08:00:17
是的,用雄辩的关系
class Category extends Model
{
protected $table="category";
protected $fillable = [
'CategoryName','Description','status'
];
function blogs(){
return $this->hasMany(Blog::class);
}
}
//Blog model
class Blog extends Model
{
protected $table="blog";
protected $fillable = [
'BlogTitle','Description','status','filepath','category','comments','rfilepath'
];
function category(){
return $this->belongsTo(Category::class);
}
}现在获取活动类别
$categories = Category::Where('status','=','active')->get();然后
@foreach($categories as $category)
@foreach($category->blogs as $blog)
{{ $blog->BlogTitle }}
@endforeach
@endforeachhttps://stackoverflow.com/questions/41083023
复制相似问题