我怎样才能在拉拉维尔中建立一排关系


How can I make a row of relations in 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) 时,我得到了一个错误?

有人可以帮助我吗?

坦克

你的关系是错误的,把它们改成:

// 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

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