如何为数组到字符串的转换提供解决方案?

时间:2018-10-18 08:15:33

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

Screenshot

型号

  

“数组到字符串的转换(SQL:更新spent_times设置updated_at = 2018-10-18 07:20:22,spent_time = 12,percentage = 60.00 ,task_category =测试id = 7)◀“

<?php

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 {

        $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->spent_time = $new_spent_time::where('task_category', 'LIKE', (array)"%$task_category%")
            ->sum('daily_spent_time', 'LIKE', (array)"%$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', 'LIKE', (array)"%$spent_time->task_category%")
            ->sum('daily_percentage', 'LIKE', (array)"%$new_spent_time->daily_percentage%", $fromDate);
        $request['percentage'] = (int)$new_spent_time->percentage + $spent_time->daily_percentage;
        $new_spent_time->save();

        return $spent_time->update($data);
    }
}

3 个答案:

答案 0 :(得分:0)

$task_category是否在"%$task_category%"数组中?

如果是这样,则会导致错误,因为无法放置数组%。

这可能是 laravel c Like with array value

的解决方案

答案 1 :(得分:0)

$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',$spent_time->task_category)
                                    ->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
$request['percentage'] = (int)$new_spent_time->percentage  + $spent_time->daily_percentage;

答案 2 :(得分:0)

您无法返回更新的查询,请尝试

return $spent_time->fresh();

,然后错误仍然存​​在,请添加为

return $spent_time->fresh()->toArray();