【发布时间】: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