Laravel BelongsTo Through 关联关系

6

如何在 Laravel 中实现 BelongsToThrough 关系?

我有一个表:

**projects_table**
id 

**categories_table**
id
project_id 

**properties_table**
id
category_id

类别属于项目

public function project(){
    return $this->belongsTo('App\Project');
}

属性属于类别。
public function category(){
    return $this->belongsTo('App\Category');
}

如何建立关联?

属性通过类别属于项目。

public function project(){
     return $this->belongsToThrough('App\Project', 'App\Category');
}
编辑问题。 因为我想在我的应用程序上实现多租户,而租户是项目。我想通过项目将类别和属性分开。
类别属于项目, 属性属于类别。
因此,我希望通过类别在属性和项目之间建立关系。 目前,我需要在类别属性表上都添加project_id,但我认为这不是正确的方法。

你遇到了哪个错误? - Lakhwinder Singh
@LakhwinderSingh,belongsToThrough不是Laravel的关系类型。我尝试按照Prafulla的答案使用hasOneThrough关系,但这并不起作用。 - Lim Socheat
1
看看这个包,它添加了 belongsToThrough 关系 https://github.com/staudenmeir/belongs-to-through - Patrick
4个回答

4
您希望使用 hasOneThrough 并指定所有键,如下所示。
public function project()
    {
        return $this->hasOneThrough(
            Project::class,
            Category::class,
            'id', # foreign key on intermediary -- categories
            'id', # foreign key on target -- projects
            'category_id', # local key on this -- properties
            'project_id' # local key on intermediary -- categories
        );
    }

这是正确的答案。 - undefined

3

我认为你可以这样做:$project->category->property。因为Project和Category之间有一对一的关系,而Category和Property之间也是一对一的关系。此外,如果您想要通过Project和Property建立关系,则可以考虑使用hasOneThrough关联。

在Project模型中:

public function property()
{
    return $this->hasOneThrough('App\Property', 'App\Category');
}

在Property模型中

public function property()
{
    return $this->hasOneThrough('App\Project', 'App\Category');
}

可能是这样

如果是这种情况,您也可以像这样做

在属性模型中

public function project()
{    
   return $this->category->project( or projects);

   // or if Category and properties have many-to-many relationship you can do 
   return $this->categories->with('projects')->get();
}

感谢您的回答,但在这种情况下我不能使用hasOneThrough关系。这是错误信息。SQLSTATE [42S22]:列不存在:在'where clause'中未知的列'categories.property_id'(SQL:select count(*) as aggregate from properties where exists (select * from projects inner join categories on categories.id = projects.category_id where categories. deleted_at is null and properties.id =categories.property_idid=1和categories.deleted_at为null和projects.deleted_at为null),并且properties.deleted_at为null) - Lim Socheat
你能告诉我,Property和Category模型之间有什么关系吗? - Prafulla Kumar Sahu
请看这个包,我想要类似于这样的东西:https://github.com/staudenmeir/belongs-to-through#usage HasManyThrough 项目 → 拥有多个 → 类别 → 拥有多个 → 属性BelongsToThrough 属性 → 属于 → 类别 → 属于 → 项目 - Lim Socheat
那么你就能够获得 $property->cateory$category->project$category->property$project->category,我说的对吗? - Prafulla Kumar Sahu
$categories->properties (hasMany),$project->categories (hasMany) - Lim Socheat
显示剩余6条评论

0

你的属性belongsTo是不正确的。该属性应属于类别。

public function category(){
    return $this->belongsTo('App\Category','category_id');
}

现在你可以像下面这样从属性中获取项目:

{{ $property->category->project->project_attribute }} 

嗨,Zahid,我更新了我的问题!我想要构建一个多租户应用程序。因此,我需要按项目过滤类别和属性。 - Lim Socheat
你正在以这种方式获得你想要的东西。使用这种方式有什么问题吗?你到底想做什么? - zahid hasan emon

0

你不能这样做。如果你想使用属性来访问项目,那么请按照我根据你的要求提供的以下步骤进行检查。

Category.php 模型

public function project(){
    return $this->belongsTo('App\Project','project_id','id');
}

Property.php 模型 这里你做错了。你不能直接访问它,你必须先与类别建立关系,然后从类别中获取项目。

Property 属于 Category

public function categories(){
    return $this->belongsTo('App\Category','category_id','id');
}

现在如果你想从控制器中访问它,那么你可以使用 with() 方法;

$property = Property::with('categories.project')->first();

在这里,您将获得$property->categories->project->name
注意:如果您想从项目中获取属性,则可以使用hasOneThrough,但是您无法使它成为反向关系。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接