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

Posted

技术标签:

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

enter image description here

Laravel 中的模型

“。”

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 谢谢,但还有一个问题,保存创建新数据时,无法计算具有相同类别的数据,我寻求帮助。拜托,你可以看到链接***.com/questions/52865862/… ***.com/questions/52884984/… 【参考方案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);
    

【讨论】:

以上是关于此集合实例上不存在属性 [task_category]的主要内容,如果未能解决你的问题,请参考以下文章

此集合实例上不存在属性 [stock]

此集合实例上不存在异常属性 [图像]

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

此集合实例上不存在属性 [名称] [重复]

Laravel - 此集合实例上不存在属性 [fileieres]

Laravel 8 Seeders:此集合实例上不存在属性 [id]