【问题标题】:Laravel 4 Pivot Table (many to many) not loadingLaravel 4数据透视表(多对多)未加载
【发布时间】: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


【解决方案1】:

我不确定我是否完全理解您的问题,但您的最后一个代码块没有意义。您需要在获取用户的同时进行操作。此外,您得到的错误是有道理的,因为 $nests (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 />";
        }
    }
});

【讨论】:

  • 谢谢...这确实有效,但我正在尝试访问数据透视表列,但此解决方案看起来正在访问嵌套表。我能够稍微重构逻辑并使用您的代码使其与嵌套表一起使用。谢谢!
猜你喜欢
  • 2017-04-11
  • 2019-02-27
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2023-02-05
  • 2014-10-31
  • 1970-01-01
  • 2014-12-13
相关资源
最近更新 更多