混合关系在哪里

时间:2019-05-01 12:23:08

标签: laravel laravel-5 jenssegers-mongodb

我试图在我现有的雄辩的生成器上添加一个where子句。该查询由SQL和MongoDB之间的混合关系组成。

构建SQL表(构建类$ connection ='mysql';)

+----+---------+--------------+-------------+----------+------------+
| id | user_id | commander_id | teamPerk_id | slot1_id | slot..._id |
+----+---------+--------------+-------------+----------+------------+
|  1 |       1 | Hero:xxxx    | Perk:xxxx   | Hero:xxx | Hero:xxx   |
+----+---------+--------------+-------------+----------+------------+

commander,teamPerk,slot1_id,slot ..._ id是mongoDB的值

class Build extends Model
{
    use HybridRelations;

    public function commander()
    {
        return $this->hasOne('\App\HeroesDB', 'gameName', 'commander_id');
    }

    public function teamPerk()
    {
        return $this->hasOne('\App\Perk', 'raw', 'teamPerk_id');
    }

    public function slot1()
    {
        return $this->hasOne('\App\HeroesDB', 'gameName', 'slot1_id');
    }

    public function slot...()
    {
        return $this->hasOne('\App\HeroesDB', 'gameName', 'slot..._id');
    }
}
class HeroesDB  extends Eloquent
{
    protected $connection = 'mongodb';
    protected $collection = 'Heroes';
    protected $fillable = ['*'];
}

不带where子句的查询,它向teamPerk,指挥官,插槽1-5返回Build集合关系(mongoDB)

$data = BuildDB::with([
    'teamPerk',
    'commander', 'slot1', 'slot2', 'slot3', 'slot4', 'slot5',
    'user',
])->withCount([
    'votes',
    'votes as up_votes' => function ($query) {
        $query->where('type', 1);
    },
    'votes as down_votes' => function ($query) {
        $query->where('type', -1);
    },
])->orderBy(DB::raw('up_votes -down_votes'), 'desc')
    ->paginate(10);

英雄文档(HeroDB)示例

{
    "_id": "5c7c1cb5146bd81498007f8e",
    "gameName": "Hero:xxxx",
    "rarity": "legendary",
    "tier": "1",
    "class": "Commando",
    "perks": [
        {
          "name": "Some Perk",
        }
     ],
     "skills": [
        {
          "name": "Some Skill",
        },
     ],
     "translated": {
        "name": "Hero name",
     }
}

问题是我如何才能在现有查询中添加where子句,例如:where perks.name =“ Some Perk”或translation.name =“ Hero name”

如果您需要更多信息,请随时询问。我的尝试是添加 ->where('commander_id', '=', "Hero:xxxx")就在->paginate()之前当然可以工作,但是我必须给出确切的“ gameName”才能使其正常工作,但是我不能将其称为'commander_id'.perks.name = "Some Perk"或任何其他MongoDB集合字段。

0 个答案:

没有答案
相关问题