在创建具有相同类别的新数据时如何按类别计算数据 [英] how to calculate data by category when creating new data with the same category

查看:108
本文介绍了在创建具有相同类别的新数据时如何按类别计算数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'));
}

查看

有问题,保存时创建新数据,然后出现错误试图获取非对象的属性"

with problems, when saving create new data then an error "Trying to get property of non-object"

但是有一个数据可以在创建新数据时保存,但是还不能计算数据,而其他类别不能那样保存,而是错误

but there is one data that can save when creating new data, but cannot yet calculate the data, and while the other categories cannot be like that, instead the error

该问题应采取哪些纠正措施?

what should be corrected from this problem?

推荐答案

更新答案

型号

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'));
}

这篇关于在创建具有相同类别的新数据时如何按类别计算数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆