【问题标题】:How can I make a row of relations in laravel?如何在laravel中建立一行关系?
【发布时间】:2015-01-20 13:00:47
【问题描述】:

我现在为此苦苦挣扎了一段时间,但我无法弄清楚它是如何工作的。 在 laravel 中,我有一些具有关系的模型。我不想拥有基于登录用户和工作区传递参数的所有帐户。

这是模型的样子:(我只是复制了方法以保持简短)

用户模型:

class User extends Eloquent implements UserInterface, RemindableInterface {

    public function workspaces()
    {
        return $this->hasMany('Workspace', 'user_id');
    }

    public function account()
    {
        return $this->hasManyThrough('account', 'Workspace', 'id', 'workspace_id');
    }
}

工作区模型:

class Workspace extends Eloquent implements UserInterface, RemindableInterface {

    public function account()
    {
        return $this->hasMany('account', 'workspace_id', 'id');
    }
    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id');
    }
}

帐户模型

class account extends Eloquent implements UserInterface, RemindableInterface {

    public function account_url()
    {
        return $this->hasOne('acountUrl', 'id', 'account_url_id');
    }
    public function workspace()
    {
        return $this->belongsTo('Workspace', 'workspace_id', 'id');
    }
}

account_url 模型

class account_url extends \Eloquent implements UserInterface, RemindableInterface {

    public function account()
    {
        return $this->belongsToMany('account', 'id', 'account_url_id');
    }
}

所以我想从具有特定工作区的登录用户那里获得所有具有 account_urls 的帐户 像这样:用户->工作区->帐户->account_url

我尝试了以下方法,但它不起作用:

$account_urls = user::find( Auth::user()->id)->first()->workspaces()->where('id', '=', 1)->account()->account_url()->select('url')->get();

和:

$account_urls = account::where('workspace_id', '=', '1')->account_url()->select('url')->get();

只有当我这样做时:

$account_urls = account::find(1)->account_url()->select('url')->get();

但是我只得到 1 个 url,但是当我为 all() 替换 find(1) 时出现错误?

有人可以帮我解决这个问题吗?

坦克,

【问题讨论】:

    标签: php laravel eloquent relationship one-to-many


    【解决方案1】:

    你的关系不对,改成:

    // User
    public function account()
    {
        return $this->hasManyThrough('Account', 'Workspace', 'user_id', 'workspace_id');
    }
    
    // Account
    // use camelCase for relations
    public function accountUrl()
    {
        // I assume you have account_url_id on accounts table
        // If it's opposite, then use hasOne
        return $this->belongsTo('AcountUrl', 'account_url_id', 'id');
    }
    
    // AccountUrl (use camelCase)
    public function account()
    {
        // if above is hasOne, then here belongsTo instead.
        return $this->hasOne('account', 'account_url_id', 'id');
    }
    

    现在,获取模型:

    // this part is .. amazing ;)
    user::find( Auth::user()->id )->first();
    
    // it does this:
    Auth::user()->id // fetch user and get his id
    user::find( .. ) // fetch user with given id, you have this user already above...
    ->first() // fetch first row from users table (not the one with id provided before)
    

    如你所愿:

    $account_urls = Auth::user()->workspaces()
       ->where('id', '=', 1)->first() // first fetches the result
       // or simply
       // ->find(1)
       ->accounts()->first()->accountUrl()
       ->pluck('url'); // this does 'SELECT url' and returns only this field instead of model
    

    请记住:

    $user->workspaces
    $workspace->accounts
    

    这些是集合,所以你不能在它们上面调用任何模型,你需要先获取单个模型。

    【讨论】:

    • tnx 供您重播,我几乎明白了,您的代码有效,但现在我只得到 1 个 url,因为我得到了第一个工作区。但是有可能我在一个工作区中有多个帐户。我该怎么做?我以为我应该把first()替换成all(),结果报错了?
    • 你不能像那样得到所有的accountUrls,因为它是Accounts的集合。而不是first 而是get 它将返回集合,然后使用foreach 例如并一一获取网址。并阅读文档,它们几乎就是您所需要的一切......
    • OK tnx 为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2021-12-17
    • 2020-06-28
    • 2020-01-18
    相关资源
    最近更新 更多