0

私は3つのテーブルを持っています:

招待、ネスト、およびユーザー。

招待には Nest_id があります。ネストには user_id があります

同じ Nest_id を持つすべてのユーザーを取得したい。これをelequentで行うにはどうすればよいですか?

モデルには has_many 関係があります


私はコントローラでこれをやろうとしています:

$users = User::with('invite')->where('invites.nest_id', '=', $id)->get();

このエラーが発生しています:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invites.nest_id' in 'where clause'

SQL: SELECT * FROM `users` WHERE `invites`.`nest_id` = ?

Bindings: array (
  0 => '2',
)

nest_id 列がないと言っていますが、あります。


class User extends Eloquent
{

public static $table = 'users';
public static $accessible = array('username', 'email', 'picture', 'password');

public function nest()
{
    return $this->has_many('Nest');
}

    public function invite()
{
     return $this->has_many('Invite');
}

class Nest extends Eloquent
{

public static $table = 'nests';

public function user()
{
    return $this->belongs_to('User');
}

public function idea()
{
    return $this->has_many('Idea');
}

public function invite()
{
    return $this->has_many_and_belongs_to('Invite');
}
4

1 に答える 1

1

Laravel リレーションシップは結合を使用しないため、リレーションシップ内の他のテーブルにはアクセスできません。「多数」を含む関係の場合、これは通常、より効率的に機能します。このようなことを逆に考える必要があることを意味します。特定の Nest を持つ Invites のユーザーが必要です。

$invites = Invite::with('user')->where('nest_id', '=', $nest_id);
foreach ($invites as $invite)
{
    $invite->user;
}
于 2013-04-18T20:08:51.317 に答える