【问题标题】:How to Make Solutions for Arrays to String Conversions?如何解决数组到字符串转换的问题?
【发布时间】:2019-03-23 00:14:57
【问题描述】:

型号

"数组到字符串的转换(SQL:更新spent_times设置updated_at=2018-10-18 07:20:22,spent_time=12,percentage=60.00,task_category=测试哪里@ 987654327@ = 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);
    }
}

【问题讨论】:

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


    【解决方案1】:
    $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】:

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

      如果是这样,则会导致错误,因为数组不能放入%。

      这可能是 laravelc Like with array value

      的解决方案

      【讨论】:

        【解决方案3】:

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

        return $spent_time->fresh();
        

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

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

        【讨论】:

        猜你喜欢
        • 2020-10-17
        • 1970-01-01
        • 1970-01-01
        • 2019-05-12
        • 1970-01-01
        • 2021-01-02
        • 2013-11-29
        相关资源
        最近更新 更多