在创建具有相同类别的新数据时如何按类别计算数据

时间:2018-10-19 09:23:21

标签: php laravel sum laravel-query-builder laravel-5.7

型号

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

查看

enter image description here

  

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

enter image description here

  

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

enter image description here

  

该问题应纠正什么?

5 个答案:

答案 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)

更新模型V.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->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)

  

当dd($ spent_time);时数据已存在

dd($spent_time);

enter image description here

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