如何使查询受集合约束

时间:2017-06-30 09:22:02

标签: php mysql laravel

我在会话中存储了以下集合:

  "trienios" => array:4 [▼
    0 => {#276 ▼
      +"curso": "TGEI"
      +"trienio": "2014-2017"
    }
    1 => {#277 ▼
      +"curso": "TGEI"
      +"trienio": "2015-2018"
    }
    2 => {#278 ▼
      +"curso": "TGEI"
      +"trienio": "2016-2019"
    }
    3 => {#279 ▼
      +"curso": "TGEI"
      +"trienio": "2013-2016"
    }
  ]

我想让查询受到集合中存在的值的约束。这是我实际拥有的查询:

$alunos = Aluno::with('trienio.curso')->select('alunos.*')->get();

这将为我提供所有alunos及其相关记录(curso和trienio)的列表。

以下是相关表格的架构:

Cursos

+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| curso | varchar(255)     | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+

Trienios

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| data_trienio | varchar(255)     | NO   |     | NULL    |                |
| curso_id     | int(11)          | NO   |     | NULL    |                |
| oe_id        | int(11)          | NO   |     | NULL    |                |
+--------------+------------------+------+-----+---------+----------------+

Alunos

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| id_cartao  | int(10) unsigned | NO   | UNI | NULL    |                |
| nome       | varchar(255)     | NO   |     | NULL    |                |
| email      | varchar(255)     | NO   | UNI | NULL    |                |
| trienio_id | int(11)          | NO   |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

以下是模型文件的相关关系:

Aluno.php

public function trienio()
{
    return $this->belongsTo('App\Trienio');
}

Trienio.php

public function curso()
{
    return $this->belongsTo('App\Curso');
}

Curso.php

public function trienio()
{
    return $this->hasMany('App\Trienio');
}

就像我在标题中所说的那样,我想让查询受到集合的约束,这样做的最佳方式是什么?

P.S - 我只想获得根据集合约束的alunos(及其各自的关系)。

1 个答案:

答案 0 :(得分:0)

这样的事情可以解决问题:

$alunos = Aluno::with('trienio.curso');

foreach ($trienios as $trienio) {
    $alunos->orWhereHas('trienio', function ($query) use ($trienio) {
        $query->where('data_trienio', $trienio['trienio'])
            ->whereHas('curso', function ($query) use ($trienio) {
                $query->where('curso', $trienio['curso']);
            });
    });
}

$alunos->get();

以上假设已经为会话中的数据设置了$trienios

希望这有帮助!