【问题标题】:Referencing to another model in Laravel引用 Laravel 中的另一个模型
【发布时间】:2017-08-30 14:33:01
【问题描述】:

刚刚在 Laravel (5.4) 中完成了我正在开发的项目的“朋友”系统。在我的情况下,我称它们为“连接”。一切正常,我现在正在尝试稍微清理一下。

从一开始,按照教程和 SO 答案,我将每个方法都放在了我的 User 模型中(这并不是那么不好:连接与用户相关)。但我想清理所有与我的连接相关的内容并将其放入一个名为 Connection 的模型中。

  1. User 模型是否应该处理与连接相关的所有事情,或者我的清洁愿望是有道理的,Connection 模型是合法的?
  2. 如何实现以下代码?如何引用User 现在我不在课堂上,$this 变得毫无意义?

连接模型

namespace App;

use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Connection extends Authenticatable
{
    const PENDING = 0;
    const ACCEPTED = 1;
    const REJECTED = 2;
}

用户模型

namespace App;

use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    // Get connections the user initiated by himself
    public function connectionsOfOwner() {
        return $this->belongsToMany(User::class, 'connections', 'user_id', 'connection_id')->withPivot('status');
    }

    // Get connections the user has been invited to
    public function connectionsOf() {
        return $this->belongsToMany(User::class, 'connections', 'connection_id', 'user_id')->withPivot('status');
    }

    // Accessor allowing to call $this->connections
    public function getConnectionsAttribute() {
        if (!array_key_exists('connections', $this->relations)) {
            $this->loadConnections();
        }

        return $this->getRelation('connections');
    }

    // Check if a Laravel relation named "connection" exists
    protected function loadConnections() {
        if (!array_key_exists('connections', $this->relations)) {
            $this->setRelation('connections', $this->mergeConnections());
        }
    }

    // Merge "connectionsOfOwner" and "connectionsOf"
    protected function mergeConnections() {
        return $this->connectionsOfOwner->merge($this->connectionsOf);
    }

    // Get pending connections
    public function getPendingConnections() {
        $filtered = $this->connections->filter(function ($value, $key) {
            if ($value->pivot->status == 0) {
                return $value;
            }
        });

        return $filtered;
    }

    // Get accepted connections
    public function getAcceptedConnections() {
        $filtered = $this->connections->filter(function ($value, $key) {
            if ($value->pivot->status == 1) {
                return $value;
            }
        });

        return $filtered;
    }

    // Add a connection
    public function addConnection($user) {
        $this->connectionsOfOwner()->attach($user->id);
    }

    // Accept a connection
    public function acceptConnection($user) {
        $this->connectionsOf()->syncWithoutDetaching([$user->id => ['status' => Connection::ACCEPTED]]);
        $this->connectionsOfOwner()->attach($user->id, ['status' => Connection::ACCEPTED]);
    }

    // Remove a connection
    public function removeConnection($user) {
        $this->connectionsOfOwner()->detach($user->id);
        $this->connectionsOf()->detach($user->id);
    }

}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您可能可以跳过连接模型并将该功能保留在您在用户模型中使用的特征“HasConnections”中。

    Jeffrey Way 在 Laracon2017 中简要介绍了这种方法:https://streamacon.com/video/laracon-us-2017/day-2-jeffrey-way

    祝你好运!

    【讨论】:

    • 嗯,谢谢,但据我所知,特征是在多个类之间复制/粘贴代码的更简单方法。不确定那是我想做的。但也许我的问题不是问题,我在User 模型中欢迎我的所有connections 东西,正如第一个问题所问的那样?
    猜你喜欢
    • 2018-09-21
    • 2011-05-13
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多