Home > database >  Where in newly create column
Where in newly create column

Time:11-16

I have newly created column

        $query->select([
            "table1.*",
            DB::raw("
                CASE
                    WHEN table2.updated_at >= table1.updated_at
                        THEN table2.updated_at
                    ELSE table1.updated_at
                END as last_update
            ")
        ]);

Now I want to find table where last_update is between since and till, my current solution:

            $query->where(function ($query) {
                $query->where("table1.updated_at", '>=', $this->since);
                $query->orWhere("table2.updated_at", '>=', $this->since);
            });
            $query->where(function ($query) {
                $query->where("table1.updated_at", '>=', $this->till);
                $query->orWhere("table2.updated_at", '>=', $this->till);
            });

How can I use 'last_update' column or use where on column with has newer update_at?

CodePudding user response:

$query->having("last_update", '>=', $this->since) Having resolved my problem.

CodePudding user response:

you can query table1 and join table2 and on both tables you can call the where clause:

Table1::with(['table2' => function ($query) {
    $query->where('updated_at', '>=', $this->since);
}])
->where('updated_at', '>=', $this->since)
->get();
  • Related