1

「users」と「nests」という 2 つのテーブルと、「nest_user」という 1 つのピボット テーブルがあります。ピボット テーブルには、フィルター処理する電子メール アドレスがあり、関連する電子メール アドレスを持つすべてのネストを取得できます。ここに私のスキーマがあります:

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('username');
        $table->text('bio');
        $table->string('picture');
        $table->string('email');
        $table->string('password');
        $table->integer('visits');
        $table->integer('nest_id');
        $table->timestamps();
    });
}

    public function up()
{
    Schema::create('nests', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('info');
        $table->integer('note_id');
        $table->integer('website_id');
        $table->integer('image_id');
        $table->integer('video_id');
        $table->integer('location_id');
        $table->integer('user_id');
        $table->integer('share_id');
        $table->string('inviteAuth');
        $table->string('tid');
        $table->timestamps();
    });
}

    public function up()
{
    Schema::create('nest_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('nest_id');
        $table->string('inviteEmail');
        $table->timestamps();
    });
}

次のようなユーザーIDに基づいて、これを問題なく実行できます。

Route::get('/t1', function () {

    $nest = User::find(2)->nest()->where('inviteEmail', '=', 'martinelli@gmail.com')->get();

    foreach( $nest as $nest)

    echo $nest->name, ': ', $nest->pivot->inviteEmail, "</br>";
});

ピボットでネストと名前と電子メールを取得できます-素晴らしい...しかし、ユーザーIDに関連付けられていない電子メールが関連付けられているすべての「ネスト」を見つけたいです。これは私を近づけていますが、まだ機能していません:

    Route::get('/t4', function () {

    $users = User::all();

    foreach($users as $users)
    {
        $nests = $users->with(array('nest' => function($query) {
        $query->where('inviteEmail', '=', 'martinelli@gmail.com');
    }))->get();

        foreach($nests->nest as $nest)
        {
            echo $nest->name,"<br />";

        }
    }

    });

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

Undefined property: Illuminate\Database\Eloquent\Collection::$nest  
4

1 に答える 1

0

あなたの質問を完全に理解しているかどうかはわかりませんが、最後のコード ブロックは意味がありません。ユーザーをフェッチしながら、 with を実行する必要があります。また、$nests (コレクション) には $nest プロパティがないため、発生しているエラーは理にかなっています。

繰り返しますが、これがあなたの求めているものかどうかはわかりませんが、これを試してみてください:

Route::get('/t4', function () {

    // Get all users and their nests where nests are filtered by inviteEmail
    $users = User::with(array('nest' => function($query) {
        $query->where('inviteEmail', '=', 'martinelli@gmail.com');
    }))->get();

    // Loop through all users
    foreach($users as $user)
    {
        // I'm asuming you defined the relation as nests() in your User model.
        foreach($user->nests as $nest)
        {
            echo $nest->name . "<br />";
        }
    }
});
于 2013-06-13T18:49:44.787 に答える