
カラムのbudgetカラム複数合算したtotalというアクセサを増やしてみる。
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* リレーション
*
* @return Illuminate\Database\Eloquent\Collection
*/
class DepartScore extends Model
{
protected $table = 'tb_department_score';
protected $primaryKey = 'department_score_id';
protected $fillable = [
'department_id',
'year',
'period',
'budget1',
'budget2',
'budget3',
'budget4',
'budget5',
'budget6',
'budget7',
'budget8',
'budget9',
'budget10',
'budget11',
'budget12',
];
$appends = ['total_budget'];
public function getTotalBudgetAttribute() {
return $this->budget1
+ $this->budget2
+ $this->budget3
+ $this->budget4
+ $this->budget5
+ $this->budget6
+ $this->budget7
+ $this->budget8
+ $this->budget9
+ $this->budget10
+ $this->budget11
+ $this->budget12;
}
}
- get<プロパティ名>Attribute()で定義
- $appendsも定義する
$appendsで定義しておくとオブジェクトでの取得を毎回してくれます。
アクセスする
$departScore = new DepartScore(); $departScore->total_budget;
->totalでアクセスできるようになる

