1

您可以在下图中看到我的数据库的一部分:

这个设计的目标是让管理员能够发送alertsusers过滤器centerfield(动态地)。如果您认为这是一个糟糕的设计,请告诉我(请说明为什么以及如何改进我的设计)。

在此处输入图像描述

现在,如果我想获取用户的警报,我应该这样做:

    $userAlerts = collect();

    auth()->user()->branch()->first()->groups()->get()->each(function ($item, $key) use ($userAlerts) {
        $item->alerts()->get()->each(function ($item, $key) use ($userAlerts) {
            $userAlerts->push($item);
        });
    });

这段代码很荒谬,我不想这样做。

相反,我想做这样的事情:

    $alerts = auth()->user()->branch()->groups()->alerts() // i want this // method 1

或者

    $alerts = auth()->user()->alerts() //or maybe this // method 2

我可以在不更改数据库的情况下做这样的事情吗?(方法 1 或 2)。

4

2 回答 2

2

您可以在分支模型上使用 laravel 的默认Has Many Through来选择关联的警报

class Branch extends Model
{

    public function alerts()
    {
        return $this->hasManyThrough(
            'App\Models\Alert',
            'App\Models\Group',
            'branch_id', // Foreign key on alert table...
            'group_id', // Foreign key on group table...
            'id', // Local key on branch table...
            'id' // Local key on group table...
        );
    }

}

使用这种方式,您可以将用户警报查询为

$alerts = auth()->user()->branch()->alerts()

或者您可以使用第三方包eloquent-has-many-deep,它可以轻松扩展hasManyThrough多个相关模型

class User extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function alerts()
    {
        return $this->hasManyDeep(
            'App\Models\Alert',
            ['App\Models\Branch', 'App\Models\Group'], // Intermediate models, beginning at the far parent (User).
            [
               'user_id', // Foreign key on the "branch" table.
               'branch_id',    // Foreign key on the "group" table.
               'group_id'     // Foreign key on the "alert" table.
            ],
            [
              'id', // Local key on the "user" table.
              'id', // Local key on the "branch" table.
              'id'  // Local key on the "group" table.
            ]
        );
    }
}

您可以直接获取用户警报

$alerts = auth()->user()->alerts()

最后,您可以通过添加一些连接来直接拉取相关警报,从而在用户模型中定义警报关系

class User extends Model
{
  
    public function alerts()
    {
        return $this->belongsTO('App\Branch', 'branch_id')
            ->join('branch_group', 'branches.id', '=', 'branch_group.branch_id')
            ->join('alerts', 'branch_group.group_id', '=', 'alerts.group_id')
            ->select('alerts.*');
    }

}
于 2020-08-22T10:15:01.650 回答
0

使用这个 repo 是我迄今为止找到的最好的解决方案。

https://github.com/johnnyfreeman/laravel-custom-relation

在这种情况下:基于文档

class User extends Model {
   use HasCustomRelations;

    public function alerts()
    {
        return  $this->custom(
            Alert::class,
            // add constraints
            function ($relation) {
                $relation->getQuery()
                    ->join('branch_group as bg', 'bg.group_id', '=', 'alerts.group_id')
                    ->join('users as u', 'u.branch_id', '=', 'bg.branch_id')
                    ->where('u.id', $this->id);
            },
            // add eager constraints
            function ($relation, $models) {
                $relation->getQuery()->whereIn('users.id', $relation->getKeys($models));
            }
        );
    }

}

这样,我可以受益于所有 laravel 关系方法。

于 2020-09-28T08:23:29.923 回答