Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tomi's avatar
Level 8

Is there a way to extend the Query Builder?

I would like to extend the Query Builder so i have my own raw Query builder class, that i can filter down. I tryd to do it like this:

namespace App\Query;

use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Processors\Processor;

class Contract extends Builder
{

public function __construct(ConnectionInterface $connection, Grammar $grammar = null, Processor $processor = null)
    {
        parent::__construct($connection, $grammar, $processor);

        $this->from('tbl_prm_contract');
    }

}

Then in my controller:

    public function getAll(ContractFilter $filter ,\App\Query\Contract $contract)
    {

        error_log($contract->where('prm_partner_id' , '=', 1)->toSql());

        error_log($contract->where('prm_partner_id' , '=', 1)->get());

    }

if i return the query i get this:

select * from "tbl_prm_contract" where "prm_partner_id" = ?

this is fine the table and field is correct. Problem is when i try to get the data and execute the query it fails with the error: Table or view notfound.

"message": "Error Code    : 942\nError Message : ORA-00942: Tabelle oder View nicht vorhanden\nPosition      : 14\nStatement     : select * from \"tbl_prm_contract\" where \"prm_partner_id\" = :p0 and \"prm_partner_id\" = :p1\nBindings      : [1,1]\n (SQL: select * from \"tbl_prm_contract\" where \"prm_partner_id\" = 1 and \"prm_partner_id\" = 1)"

Any one got an idea?

0 likes
5 replies
martinbean's avatar

@Tomi The query builder is an abstraction for building SQL queries. Eloquent models use it under the hood (any method not defined in a model will be passed to the query builder).

What is it you’re trying to do that you don’t think you can do with an Eloquent model/the query builder as is?

1 like
Tomi's avatar
Level 8

I have a big data structure with many joins over different tables. Where i think eloquent would not be enough.

Also i read somewhere that Eloquent and joins arent the best friends.

jlrdw's avatar

Write a normal query using getPdo ()

1 like
Tomi's avatar
Level 8

@martinbean So what i actually did now is a simple class where i create a query and when i call a function this query get returned and it can be filtered by the filter class then selected.

namespace App\Query;


use Illuminate\Support\Facades\DB;

class Contract
{

    protected $query;

    public function __construct()
    {
        $this->initQuery();
    }

    public function initQuery()
    {
        $this->query = DB::table('tbl_prm_contract');

    }

    public function getQuery()
    {
        return $this->query;
    }

}

In the Controller:

    public function getAll(ContractFilter $filter ,\App\Query\Contract $contract)
    {

        $this->contractRepo = new ContractRepository($contract->getQuery());

        return $this->contractRepo
            ->filter($filter)
            ->advancedFilter(\request()->get('data'), $filter)
            ->getAll();
    }
martinbean's avatar
Level 80

@Tomi Eloquent can handle joins.

I think this lesson may be appropriate if you’re wanting to filter model results by building up query clauses.

1 like

Please or to participate in this conversation.