mercredi 1 mai 2019

Where on hybrid relations (with)

I am trying to add a where clause on my existing eloquent builder. The query consists of a hybrid relation between SQL and MongoDB.

Builds SQL table (Build Class $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 are values for mongoDB

Build Class

    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 = ['*'];
}

The query, without where clause, it returns a Build collection relations (mongoDB) to teamPerk, commander, slot1-5

 $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);

Example of a hero document (HeroDB)

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

The question is how can I manage to add to the existing query a where clause ex: Where perks.name = "Some Perk" or translated.name = "Hero name"

If you require more information feel free to ask. My attempt was adding ->where('commander_id', '=', "Hero:xxxx") just before ->paginate() of course it will work, but I would have to give the exact "gameName" for it to work, but I can't refer to it as where 'commander_id'.perks.name = "Some Perk" or any other MongoDB collection field.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire