【问题标题】:how to calculate data by category when creating new data with the same category创建具有相同类别的新数据时如何按类别计算数据
【发布时间】:2019-03-24 04:13:27
【问题描述】:

型号

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

查看

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

但是有一个数据在创建新数据的时候可以保存,但是还不能计算数据,而其他类不能这样,反而报错

应该从这个问题中纠正什么?

【问题讨论】:

  • $new_spent_time 根本没有被保存。您还在创建更新properties 和数组keys。选择一个。
  • 那么应该改进什么?对不起,我还在学习,所以还是不明白你的意思。
  • 结构有点难以理解,我看了看,但对你在哪里使用where() 子句感到困惑。您可以在一条记录上运行where()
  • 那么要改变什么?
  • 您能否用您的模型和模型关系更新您的问题,以便我了解要实现的目标。

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


【解决方案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);
    }
}

控制器

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

【讨论】:

    【解决方案2】:

    数据在那里,当 dd($spent_time);

    dd($spent_time);
    

    【讨论】:

      【解决方案3】:

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

      在这里,我们为基于相关task_category的所有记录创建两个accessors,一个用于获取每日花费时间,另一个用于获取每日百分比。

      可以使用以下方式调用:

      $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() 方法。

      希望这会有所帮助。

      【讨论】:

      • 对控制器有额外的调用吗?
      • store() 控制器方法中只需要正常保存。 dailySpentTime 可以在任何 SpentTime 记录上调用。
      • 已更新。如果您理解,请告诉我。
      • 错误Call to a member function getDailySpentTimeAttribute() on boolean
      • 你能dd() $spent_time 看看回显的内容吗?
      【解决方案4】:

      更新模型

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

      【讨论】:

        猜你喜欢
        • 2019-03-27
        • 2019-03-22
        • 1970-01-01
        • 2021-09-14
        • 2018-03-31
        • 1970-01-01
        • 2018-11-19
        • 2019-10-22
        • 1970-01-01
        相关资源
        最近更新 更多