【问题标题】:Property [task_category] does not exist on this collection instance此集合实例上不存在属性 [task_category]
【发布时间】:2019-03-23 22:57:10
【问题描述】:

enter image description here

Laravel 中的模型

“此集合实例上不存在属性 [task_category]。”

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::get();
    $new_spent_time->task_category = (['{task_category}' => $new_spent_time->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);
        $request['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);
        $request['percentage'] = (int)$new_spent_time->percentage  + $spent_time->daily_percentage;
        return $spent_time->update($data);
    }
}

【问题讨论】:

  • 您的spenttime 表迁移中有task_category 字段吗?
  • @thisiskelvin 是的,当然
  • 您的SpentTime::get() 函数的用途是什么?是为了获得一审吗?创建一个新的?如果要获得第一个,请使用::first()
  • @thisiskelvin 谢谢,但还有一个问题,保存创建新数据时,无法计算具有相同类别的数据,我寻求帮助。拜托,你可以看到链接stackoverflow.com/questions/52865862/…

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


【解决方案1】:

我通过使用first() 而不是get() 解决了这个问题:

$new_spent_time = SpentTime::first();

【讨论】:

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

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2020-07-11
      • 2017-05-12
      • 2018-10-12
      • 2021-02-08
      • 2021-12-16
      • 2021-09-14
      相关资源
      最近更新 更多