「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