Laravel使用数据透视表作为BelongsToMany()获取帖子类别

时间:2017-12-07 11:07:00

标签: laravel laravel-5

在我的数据库中,我有contents_categoriescontent表与BelongsToMany()关系

Contents

class Contents extends Model
{
    protected $table = 'contents';
    protected $guarded = ['id'];

    public function categories()
    {
        return $this->belongsToMany(ContentCategories::class);
    }
}

ContentCategories

class ContentCategories extends Model
{
    protected $table = 'contents_categories';
    protected $guarded = ['id'];

    public function contents()
    {
        return $this->belongsToMany(Contents::class);
    }
}

使用此代码将数据存储在contents表中,选定的多个类别可以正常工作,并可以content_categories_contents

保存到数据库和数据透视表中
public function store(RequestContents $request)
{
    $data = Contents::create([
        'title' => $request->title,
        'lang' => $request->_language,
        'type' => $request->type,
        'description' => $request->description,
        'featured_images' => '',
        'visit' => 0,
    ]);

    $data->categories()->attach($request->categories);
}

enter image description here

现在!不幸的是,当我尝试使用此代码获取内容类别时,我没有结果:

public function show($id)
{
    $content = Contents::find($id);
    $selected_categories = $content->categories();
    dd($selected_categories);
}

结果:

BelongsToMany {#208 ▼
  #table: "content_categories_contents"
  #foreignPivotKey: "contents_id"
  #relatedPivotKey: "content_categories_id"
  #parentKey: "id"
  #relatedKey: "id"
  #relationName: "categories"
  #pivotColumns: []
  #pivotWheres: []
  #pivotWhereIns: []
  #pivotCreatedAt: null
  #pivotUpdatedAt: null
  #using: null
  #accessor: "pivot"
  #query: Builder {#217 ▼
    #query: Builder {#209 ▶}
    #model: ContentCategories {#213 ▼
      #table: "contents_categories"
      #guarded: array:1 [▶]
      #connection: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: false
      +wasRecentlyCreated: false
      #attributes: []
      #original: []
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
    #eagerLoad: []
    #localMacros: []
    #onDelete: null
    #passthru: array:11 [▶]
    #scopes: []
    #removedScopes: []
  }
  #parent: Contents {#218 ▼
    #table: "contents"
    #guarded: array:1 [▶]
    #connection: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #changes: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
  }
  #related: ContentCategories {#213 ▶}
}

1 个答案:

答案 0 :(得分:2)

这是因为您定义的方法正是返回您定义的方法以返回BelongsToMany对象。这些是构建器类型对象,因此您可以查询关系。它们不是它的结果。

你期待它看起来像加载的关系。您必须自己查询关系,或点击"动态属性"对于将加载它​​的关系,如果它尚未加载并返回结果。

$model->categories();   // returns what you defined it to return
$model->categories()->where(...)->get();

$model->categories;   // loaded relationship via dynamic property

Laravel 5.5 Docs - Eloquent - Relationships - Relationship Methods vs Dynamic Properties