public static function findOrCreate($plan_id, $data)
{
$fromDate = Carbon::now()->subDay()->startOfWeek();
$nowDate = Carbon::now()->today();
$spent_time = static::where('plan_id', $plan_id)->first();
if (is_null($spent_time)) {
return static::create($data);
}else{
$new_spent_time = SpentTime::find($plan_id);
$task_category = $new_spent_time->task_category;
$new_spent_time->task_category = (['{task_category}' => $task_category,
'{daily_spent_time}' => $new_spent_time->daily_spent_time,
'{daily_percentage}' => $new_spent_time->daily_percentage,
'{spent_time}' => $new_spent_time->spent_time,
'{percentage}' => $new_spent_time->percentage, $new_spent_time->task_category]);
$new_spent_time->spent_time = $new_spent_time::where('task_category',$task_category)
->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate);
$new_spent_time['spent_time'] = (int)$new_spent_time->spent_time + $spent_time->daily_spent_time;
$new_spent_time->percentage = $new_spent_time::where('task_category',$task_category)
->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
$new_spent_time['percentage'] = (int)$new_spent_time->percentage + $spent_time->daily_percentage;
return $spent_time->update($data);
}
}
public function store(Request $request)
{
$spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
'plan_id' => $request->get ('plan_id'),
'daily_spent_time' => $request->get ('daily_spent_time'),
'daily_percentage' => $request->get ('daily_percentage'),
'reason' => $request->get ('reason'),
]);
return redirect()->route('real.index', compact( 'spent_time'));
}
有问题,保存时创建新数据,然后出现错误“试图获取非对象的属性”
但是有一个数据可以在创建新数据时保存,但是还不能计算数据,而其他类别不能那样保存,而是错误
该问题应纠正什么?
答案 0 :(得分:1)
在您的SpentTime
模型中,您可以创建accessors,这些函数可在此处用于查询所有相关记录的每日总和:
public function getDailySpentTimeAttribute()
{
return self::where('task_category_id', $this->task_category_id)
->get()
->sum('daily_spent_time');
}
public function getDailyPercentageAttribute()
{
return self::where('task_category_id', $this->task_category_id)
->get()
->sum('daily_percentage');
}
在这里,我们根据相关的accessors
为所有记录创建两个task_category
,一个用于获取每日花费的时间,另一个用于获取每日百分比。
可以使用以下命令进行调用:
$dailySpentTime = SpentTime::find($id)->dailySpentTime;
// or within your blade template
{{ $spentTime->dailySpentTime }}
在您的控制器内,由于保存时不再需要运行任何计算,因此您可以执行以下操作:
public function store(Request $request)
{
$spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
'task_category' => $request->get('task_category'),
'reason' => $request->get('reason'),
]);
return redirect()->route('real.index', compact('spent_time'));
}
请确保删除当前覆盖laravel版本的自定义findOrCreate()
方法。
希望这会有所帮助。
答案 1 :(得分:0)
public static function findOrCreate($plan_id, $data)
{
$fromDate = Carbon::now()->subDay()->startOfWeek();
$nowDate = Carbon::now()->today();
$spent_time = static::where('plan_id', $plan_id)->first();
if (is_null($spent_time)) {
return static::create($data);
}else{
$new_spent_time = SpentTime::first();
$task_category = $new_spent_time->task_category;
$new_spent_time->task_category = (['{task_category}' => $task_category,
'{daily_spent_time}' => $new_spent_time->daily_spent_time,
'{daily_percentage}' => $new_spent_time->daily_percentage,
'{spent_time}' => $new_spent_time->spent_time,
'{percentage}' => $new_spent_time->percentage, $new_spent_time->task_category]);
$new_spent_time->spent_time = $new_spent_time::where('task_category',$task_category)
->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate);
$new_spent_time['spent_time'] = (int)$new_spent_time->spent_time + $spent_time->daily_spent_time;
$new_spent_time->percentage = $new_spent_time::where('task_category',$task_category)
->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
$new_spent_time['percentage'] = (int)$new_spent_time->percentage + $spent_time->daily_percentage;
return $spent_time->update($data);
}
}
答案 2 :(得分:0)
现在是新问题,可以保存创建新数据,但不能按相同类别计算数据
public static function findOrCreate($plan_id, $data)
{
$fromDate = Carbon::now()->subDay()->startOfWeek();
$nowDate = Carbon::now()->today();
$spent_time = static::where('plan_id', $plan_id)->first();
if (is_null($spent_time)) {
return static::create($data);
}else{
$new_spent_time = SpentTime::first();
$task_category = $new_spent_time->task_category;
$new_spent_time->spent_time = $new_spent_time::where('task_category',$task_category)
->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate);
$new_spent_time['spent_time'] = $new_spent_time->spent_time + $spent_time->daily_spent_time;
$new_spent_time->percentage = $new_spent_time::where('task_category',$task_category)
->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
$new_spent_time['percentage'] = $new_spent_time->percentage + $spent_time->daily_percentage;
$new_spent_time->save();
return $spent_time->update($data);
}
答案 3 :(得分:0)
答案 4 :(得分:0)
public static function findOrCreate($plan_id, $data)
{
$spent_time = static::where('plan_id', $plan_id)->first();
$task_category = $spent_time->task_category;
if (is_null($spent_time)) {
return static::create($data);
}else{
$spent_time['spent_time'] = $spent_time->spent_time + $spent_time->daily_spent_time;
$spent_time['percentage'] = $spent_time->percentage + $spent_time->daily_percentage;
return $spent_time->update($data);
}
}
public function index()
{
$spent_times = SpentTime::orderBy('task_category')->where('created_at', '>=', Carbon::today()->toDateString())->paginate(10);
$user_stories = Plan::pluck('user_story', 'id');
$real = new SpentTime;
return view('reals.index', compact('spent_times', 'user_stories', 'real'));
}
public function store(Request $request)
{
$spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
'plan_id' => $request->get ('plan_id'),
'daily_spent_time' => $request->get ('daily_spent_time'),
'daily_percentage' => $request->get ('daily_percentage'),
'reason' => $request->get ('reason'),
]);
return redirect()->route('real.index', compact( 'spent_time'));
}