在laravel中创建新数据时无法按类别保存计算数据

Posted

技术标签:

【中文标题】在laravel中创建新数据时无法按类别保存计算数据【英文标题】:Cannot save calculation data by category when create new data in laravel 【发布时间】:2019-03-22 18:45:04 【问题描述】:

模型花费时间

public static function findOrCreate($plan_id, $data)

    $fromDate = Carbon::now()->subDay()->startOfWeek()->toDateString();
    $nowDate = Carbon::now()->today()->toDateString();

    $spent_time = static::where('plan_id', $plan_id)->first();

    if (is_null($spent_time)) 
        return static::create($data);
    else
        $task_category = SpentTime::where('task_category', $spent_time->task_category)->get();
        $create_spent_times = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_spent_time', $fromDate);
        $request['spent_time'] = (int)$create_spent_times + $spent_time->daily_spent_time;

        $create_spent_times = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_percentage', $fromDate);
        $request['percentage'] = (int)$create_spent_times + $spent_time->daily_percentage;

        $spent_time->save();
        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'));

【问题讨论】:

您面临的问题是什么?你有任何错误吗? 错误信息是什么? @linuxartisan 将之前创建的基于同一类别的值进行汇总,当您想在同一类别中创建新数据时。 @AriPratomo 没有错误,但是可以根据分类计算数据,只能保存数据,不能计算数据 $request数组数据在findorCreate()函数中是做什么用的? 【参考方案1】:

型号

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

【讨论】:

【参考方案2】:

更新答案

可以新建数据,但不能按类别计算数据

型号

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

控制器

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

【讨论】:

以上是关于在laravel中创建新数据时无法按类别保存计算数据的主要内容,如果未能解决你的问题,请参考以下文章

按相同类别创建新数据时无法保存数据

如何在 Laravel 中创建新用户?

text 在Laravel中创建新的迁移

text 在Laravel中创建新模型

text 在Laravel中创建新的控制器

text 在Laravel中创建新的控制器,模型和迁移