【发布时间】:2013-06-11 15:32:20
【问题描述】:
我有 2 个表:“users”和“nests”以及 1 个数据透视表:“nest_user”。在我的数据透视表中,我有要过滤的电子邮件地址,以便我可以获取所有具有关联电子邮件地址的嵌套。这是我的方案:
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
【问题讨论】:
-
希望有人能帮忙。这是我在使用 Laravel 时遇到的最后一个大逻辑/语法障碍。我已经根据文档尝试了所有配置,但仍然没有成功。任何见解都会很棒。我觉得我很接近。
标签: laravel laravel-4 eloquent