samedi 31 mars 2018

Laravel Union 2 Tables

I have the following custom builder (I need to supply a builder instnace to a package that I use)

public function query()
{
    $connection = $this->getConnection();

    return $connection->table(
        $connection->raw('(' . $this->mainQuery()->toSql() . ') abc')
    )->select('*');
}

public function mainQuery()
{
    $q1 = $this->firstQuery();
    $q2 = $this->secondQuery();

    return $q1->unionAll($q2);
}

private function firstQuery()
{
    return $this->topic
        ->where('title', 'LIKE', '%'.'test'.'%')
        ->select(['message', 'created_at']);

}

private function secondQuery()
{
    return $this->reply
        ->where('message', 'LIKE', '%'.'test'.'%')
        ->select(['message', 'created_at']);
}

Doing a

 $builder = app(QueryBuilderHelper::class);
 $query = $builder->query();
 return $this->applyScopes($query);

results in

General error: 2031 (SQL: select count(*) as aggregate from (select * from ((select `message`, `created_at` from `topics` where `title` LIKE ? and `topics`.`deleted_at` is null) union all (select `message`, `created_at` from `replies` where `message` LIKE ? and `replies`.`deleted_at` is null)) abc) count_row_table)"}

I don't see what the issue is..



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2pTz6Od
via IFTTT

Aucun commentaire:

Enregistrer un commentaire