Laravel 4数据透视表(多对多)不加载 [英] Laravel 4 Pivot Table (many to many) not loading

查看:134
本文介绍了Laravel 4数据透视表(多对多)不加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表:'users'和'nests'和1个数据透视表:'nest_user'。在我的数据透视表中,我有一个要过滤的电子邮件地址,所以我可以抓住所有具有相关电子邮件地址的巢穴。这是我的scehma:

  public function up()
{
Schema :: create ',function(Blueprint $ table)
{
$ table-> increment('id');
$ table-> string('username');
$ table-> text('bio');
$ table-> string('picture');
$ table-> string('email');
$ table- > string('password');
$ table->整数('visits');
$ table->整数('nest_id');
$ table->时间戳();
});
}

public function up()
{
Schema :: create('nests',function(Blueprint $ table)
{
$ table-> increment('id');
$ table-> string('name');
$ table-> string('info');
$ table-> integer('note_id');
$ table-> integer('website_id');
$ table-> integer('image_id');
$ table- >整数('video_id');
$ table->整数('location_id');
$ table->整数('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-> increment('id');
$ table->整数('user_id');
$ table->整数('nest_id');
$ table-> string('inviteEmail');
$ table-> timestamps();
});
}

我可以根据这样的用户ID进行此操作:

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

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

foreach作为$ nest)

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

我可以得到巢和名字和电子邮件在枢轴 - 真棒...但是,我想要找到所有的巢穴,其中没有与用户标识相关联的电子邮件。这是让我更近,但仍然不起作用:

  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 />;

}
}

});

我收到此错误:

 未定义的属性:Illuminate\Database\Eloquent\Collection :: $ nest 


解决方案

我不知道我完全理解你的问题,但是你最后一个代码块没有意义。您将在抓取用户时执行此操作。另外,你收到的错误是有道理的,因为$ nests(Collection)没有$ nest属性。



再次,我不知道这是你以后,但尝试一下:

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

//通过inviteEmail过滤嵌套的所有用户及其巢穴
$ users = User :: with(array('nest'=> function($ query){
$ query-> where('inviteEmail','=','martinelli@gmail.com');
})) - > get();

//循环遍历所有用户
foreach($ users as $ user)
{
/ /我在你的用户模型中将关系定义为nests()
foreach($ user->嵌套为$ nest)
{
echo $ nest-> name 。< br />;
}
}
});


I have 2 tables: 'users' and 'nests' and 1 pivot table: 'nest_user'. In my pivot table I have email addresses I want to filter against so I can grab all the nests which have the associated email addreses. Here is my scehma:

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();
    });
}

I can do this no problem based on the User ID like this:

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>";
});

I can get the nest and name and the email in the pivot - awesome...However, I want to find ALL the 'nests' which have the associated email which is NOT tied to a user ID. This is getting me closer but it is still not working:

    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 />";

        }
    }

    });

I am getting this error:

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

解决方案

I'm not sure I fully understand your question, but your last code block doesn't make sense. You'll want to do the with while fetching the users. Also, the error you're getting makes sense, because the $nests (Collection) doesn't have a $nest property.

Again, I'm not sure this is what you're after, but give this a try:

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 />";
        }
    }
});

这篇关于Laravel 4数据透视表(多对多)不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆