如何在laravel中建立一排关系?

时间:2014-11-22 12:23:59

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

我现在正在努力解决这个问题,但我无法弄清楚它是如何运作的。 在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的所有帐户 像这样的东西:user-> workspace-> account-> 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个网址,但是当我为所有()重新找到find(1)时,我收到错误?

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

坦克,

1 个答案:

答案 0 :(得分:0)

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

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

这些是集合,因此您无法在其上调用任何模型,您需要先获得单个模型。