无法保存新数据,并且无法将Array转换为字符串? [英] can't save new data, and Array to string conversion?

查看:67
本文介绍了无法保存新数据,并且无法将Array转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组到字符串的转换(SQL:更新 spent_times set updated_at = 2018-10-18 06:02:29, spent_time =12,百分比 = 60.00, task_category =测试,其中 id = 7)◀"

"Array to string conversion (SQL: update spent_times set updated_at = 2018-10-18 06:02:29, spent_time = 12, percentage = 60.00, task_category = testing where id = 7) ◀"

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', $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 = SpentTime::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;
        $new_spent_time->save();
        return $spent_time->update($data);
    }
}

laravel中的控制器

在函数存储中,创建新数据时无法按类别保存数据,也不能按类别计算数据,应计算数据并以pict的形式输入到表中.

Controller in laravel

in function store, can't save data and can't calculation data by category when create new data, data should be calculated and entered into the table as pict.

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->percentage = SpentTime::where('task_category', $spent_time->task_category)->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);

这里您将 $ spent_time-> task_category (一个数组)分配给'task_category'(一个字符串字段).

Here you assigning $spent_time->task_category (an array) to 'task_category' (a string field).

更改上述变量的值将解决您的问题.

Change value of above variable it will fix your issue.

这篇关于无法保存新数据,并且无法将Array转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆