'Append virtual static column/field in Laravel query?
I have two scenarios to implement in my query from the same database table and show them in two different front-end tables. The problem is I need to implement both of the scenarios in a single query maybe with union.
I want to know if the current result belongs to scenario 1 or scenario 2. For that purpose, I want to add a virtual column in my query like scenario 1 or the scenario 2 to identify the results. Is it possible with laravel eloquent or raw query?
$a = orderModel::virtualColumn('scenario-1')
$b = orderModel::virtualColumn('scenario-2')
->union($a)
->get();
$result = $b;
Solution 1:[1]
Using Virtual Columns in Laravel - Accessors and Appends
class Order extends Model
{
$appends = [
'scenario'
];
public function getScenarioAttribute()
{
return $this->fields_one . " " . $this->fields_two;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexander Burcev |
