Laravel通过模型实例交往关系

发布于 2025-02-14 01:44:11 字数 850 浏览 0 评论 0原文

在我的Laravel 9项目中,我的用户模型属于3个模型(分销商,代理商和广告商),例如:

public function distributor()
{
    return $this->belongsTo(Distributor::class);
}

public function agency()
{
    return $this->belongsTo(Agency::class);
}

public function advertiser()
{
    return $this->belongsTo(Advertiser::class);
}

我使用此功能在我的Distributorrepository(以及其他2个模型的存储库)中关联:

public function associateUser($id, $user_id)
{
    $user        = $this->user->find($user_id);
    $distributor = $this->find($id);
    $result      = $user->distributor()->associate($distributor)->save();
    return $result;
}

现在,我想修改它们以将它们关联起来通过用户的ID和关系模型实例(分销商,代理商和广告商)在我的用户介绍(或用户服务)中动态上,例如:

public function associate($id, $modelInstance)
{
    // Something happens here
}

这是可能的吗?谢谢!

In my Laravel 9 project, My User Model is belongs to 3 models (Distributor, Agency and Advertiser) like:

public function distributor()
{
    return $this->belongsTo(Distributor::class);
}

public function agency()
{
    return $this->belongsTo(Agency::class);
}

public function advertiser()
{
    return $this->belongsTo(Advertiser::class);
}

And I use this function to associate in my DistributorRepository(and other 2 model's repositories):

public function associateUser($id, $user_id)
{
    $user        = $this->user->find($user_id);
    $distributor = $this->find($id);
    $result      = $user->distributor()->associate($distributor)->save();
    return $result;
}

Now, I want to modify to associate them by user's id and relation model instance (Distributor, Agency and Advertiser) in my UserRepository (or UserService) dynamically, like:

public function associate($id, $modelInstance)
{
    // Something happens here
}

Is that possible and how to do? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2025-02-21 01:44:11

如果我理解正确,那么您可以这样做:

public function associate($user_id, $parentModelInstance)
{
    $user = User::find($user_id); //or how ever you get user in repository
    $reflect = new ReflectionClass($parentModelInstance);
    $relationName = Str::lower($reflect->getShortName()); //magic for getting relation (if class name is relation name)

    return $user->{$relationName}()->associate($parentModelInstance)->save();
}

您可以这样使用:

$distributor = Distributor::find(1);
$user_id = 1;

$this->associate($user_id, $distributor)

If I understand right, then you can do it with:

public function associate($user_id, $parentModelInstance)
{
    $user = User::find($user_id); //or how ever you get user in repository
    $reflect = new ReflectionClass($parentModelInstance);
    $relationName = Str::lower($reflect->getShortName()); //magic for getting relation (if class name is relation name)

    return $user->{$relationName}()->associate($parentModelInstance)->save();
}

And you can use it like this:

$distributor = Distributor::find(1);
$user_id = 1;

$this->associate($user_id, $distributor)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文