我正在尝试创建一个计算特定项目费用的函数。
模型费用属于项目和用户
protected $fillable=[
'name','payment_method','currency','value','date','description','user_id','project_id',
];
public function project(){
return $this->belongsTo(Project::class);
}
public function user(){
return $this->belongsTo(User::class);
}
和Project有很多用户和费用
protected $fillable = [
'name', 'description', 'start_date', 'end_date', 'value', 'client_id','currency','',
];
public function expenses(){
return $this->hasMany(Expense::class);
}
public function users(){ //was user before
return $this->belongsToMany(User::class,"user_projects");
}
helpers.php中的函数是这个
function expenses_per_project(){
//dont understand how to do this. so sry.
}
当用户添加费用时,它是这样的: expense add
费用仪表板是这样的: Expenses Dashboard
我无法理解如何使用该功能,所以我们非常欢迎您提供一些帮助!
此外,每个项目都属于许多用户,费用也取决于他们的工资。
public function wages(){
return $this->hasMany(UserWage::class,'user_id');
}
public function projects(){
return $this->hasMany(Project::class);
}
答案 0 :(得分:0)
你可以这样做,我不确定你的Expense模型有哪些列,但是你需要把保存费用值的列的名称作为sum函数的参数。
function expenses_per_project_and_user($project_id, $user_id) {
$project = Project::find($project_id);
return (float) $project->expenses()->where('user_id', $user_id)->sum('value_column');
}
还有一个额外的片段,每个用户的项目费用
interface Person {
name: string;
age: number;
}
interface Car {
maxv: number;
brand: string;
}