Laravel 4数据透视表(多对多)未加载
我有两个表:“用户”和“嵌套”,还有一个数据透视表:“嵌套用户”。在我的pivot表中,我有我想要过滤的电子邮件地址,因此我可以抓取所有具有关联电子邮件地址的嵌套。这是我的权杖:Laravel 4数据透视表(多对多)未加载,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我有两个表:“用户”和“嵌套”,还有一个数据透视表:“嵌套用户”。在我的pivot表中,我有我想要过滤的电子邮件地址,因此我可以抓取所有具有关联电子邮件地址的嵌套。这是我的权杖: public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('username');
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>";
});
我不确定我是否完全理解您的问题,但您的最后一个代码块没有意义。在获取用户时,您将需要使用。此外,您得到的错误是有意义的,因为$nest(集合)没有$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 />";
}
}
});
Route::get('/t4',函数(){
//获取所有用户及其巢穴,通过电子邮件过滤巢穴
$users=User::with(数组('nest'=>函数($query){
$query->where('inviteEmail','=','martinelli@gmail.com');
}))->get();
//遍历所有用户
foreach($users作为$user)
{
//我估计您在用户模型中将关系定义为nests()。
foreach($user->nests as$nest)
{
echo$nest->name.“
”;
}
}
});
我希望有人能帮忙。这是我在Laravel身上遇到的最后一个逻辑/语法障碍。我已经尝试了基于文档的所有配置,但仍然没有成功。任何洞察都会很好。我觉得我已经很接近了。谢谢……这确实有效,但我正在尝试访问透视表列,但这个解决方案看起来像是在访问嵌套表。我能够稍微重构逻辑,并使用您的代码使其与嵌套表一起工作。谢谢
Undefined property: Illuminate\Database\Eloquent\Collection::$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 />";
}
}
});