简体   繁体   English

Laravel雄辩的3表联接查询

[英]Laravel Eloquent 3 Table Join Query

I have 3 migration 我有3个迁移

Default User migration, Event migration and Share migration. 默认用户迁移,事件迁移和共享迁移。

Schema::create('events', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('owner');
        $table->dateTime('start_date');
        $table->dateTime('end_date');
        $table->dateTime('alert_date');
        $table->string('title');
        $table->text('description');
        $table->string('local');
        $table->timestamps();

        $table->foreign('owner')->references('id')->on('users');
    });

Schema::create('shares', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('host');
        $table->unsignedInteger('guest');
        $table->boolean('host_answer')->nullable();
        $table->boolean('guest_answer')->nullable();;
        $table->timestamps();

        $table->foreign('host')->references('id')->on('users');
        $table->foreign('guest')->references('id')->on('users');
    });

Then i have the relationships established on the respective models. 然后,我在各个模型上建立了关系。

Users: 用户:

public function events()
{
    return $this->hasMany(Event::class);
}

public function host()
{
    return $this->hasMany(Share::class);
}

public function guest()
{
    return $this->hasMany(Share::class);
}

Event: 事件:

public function userOwner()
{
    return $this->belongsTo(User::class, 'owner');
}

Share: 分享:

public function userGuest()
{
    return $this->belongsTo(User::class, 'guest');
}

I need all distinct events where at least one of 3 conditions are meet: 我需要满足以下3个条件中的至少一个的所有不同事件:

1- events.owner = $someUserId. 1- events.owner = $ someUserId。

2- shares.host = $someUserId and shares.guest_answer = 1. 2-shares.host = $ someUserId,shares.guest_answer = 1。

3- shares.guest = $someUserId and shares.guest_answer = 1. 3-shares.guest = $ someUserId,shares.guest_answer = 1。

If this was simply SQL i guess i would be able to make the query... but is laravel so i'm having some troubles with my query. 如果这只是简单的SQL,我想我可以进行查询...但是是laravel,所以我的查询遇到了一些麻烦。

This is what i get: 这就是我得到的:

 $events = Event::join('users', 'events.owner', '=', 'users.id')
                ->join('shares', 'users.id', '=', 'shares.host')
                ->where  ([[ 'events.owner', $userId ]])
                ->orWhere([[ 'shares.host', $userId],
                           [ 'shares.guest_answer', '1' ]])
                ->orWhere([[ 'shares.guest', $userId],
                           [ 'shares.guest_answer', '1' ]])
                ->paginate(10);

But this query is simply returning the events where events.owner = $userId. 但是此查询只是返回其中events.owner = $ userId的事件。

Thank you very much for you time. 非常感谢您抽出宝贵的时间。

    $userId = Auth::id();

    $events = Event::join('users'       , 'events.owner', '=', 'users.id')
                   ->join('shares as sg', 'users.id'    , '=', 'sg.guest')
                   ->join('shares as sh', 'users.id'    , '=', 'sh.host' )

                   ->Where([   ['events.owner'    , '=', $userId] ])

                   ->orWhere([ ['sg.host'         , '=', $userId] ,
                               ['sg.guest_answer' , '=', '1']     ])

                   ->orWhere([ ['sh.guest'        , '=', $userId] ,
                               ['sh.guest_answer' , '=', '1']     ])

                    ->paginate(10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM