Search code examples
phpmysqllaravelbuilderrevisionable

Laravel Query Builder use variables in MySQL for complex select?


How to do this with one mysql request:

$revision = $this->Revision->where('batch', $batch)->first();

$revisions = $this->Revision->where('batch','>', $batch)
->where('revisionable_type', $revision->revisionable_type)
->where('revisionable_id', $revision->revisionable_id)
->get();

$this->Revision = eloquent model;

others are just columns.


Solution

  • I misunderstood your question initially. You can do this by using:

    $sql = "SELECT * FROM revisions AS tmpa 
            INNER JOIN revisions AS tmpb 
            ON tmpb.revisionable_type = tmpa.revisionable_type 
            AND tmpb.revisionable_id = tmpa.revisionable_id 
            WHERE tmpa.batch = '$batch'";
    
    DB::select(DB::raw($sql));