How can I query using Accessor that I defined in Model
Model.php
public function getStatusAttribute()
{
if($this->profit == 0 && $this->loss == 0){
return 'Not Traded';
}elseif($this->loss > $this->profit ) {
return 'Loss';
}elseif($this->profit == $this->loss){
return 'Neutral';
}else{
return 'Profit';
}
}
Controller.php
public function get_model_status()
{
// how can i query based on accessor that i defined in Model .
}
i want to query all data that has status " Profit " . i get an error for this method . Column not found: 1054 Unknown column 'status' . Note that , i didnt have any attribute for "status" . Stock "status" based on getStatusAttribute() method .
Stock.php
public function scopeProfit($query)
{
return $query->where('status','Profit');
}
Controller.php
$top_profit = Stock::profit()->get();
based on my understanding and your suggestion method . i need to add a new column for profit and loss . am i right ? define the status based on that . am i right ?