【问题标题】:can't save new data, and Array to string conversion?无法保存新数据,以及数组到字符串的转换?
【发布时间】:2019-03-22 21:49:06
【问题描述】:

laravel 中的模型

"数组到字符串的转换(SQL:更新spent_times设置updated_at=2018-10-18 06:02:29,spent_time=12,percentage=60.00,task_category=测试哪里@ 987654328@ = 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 中的控制器

在函数库中,新建数据时不能保存数据,也不能按类别计算数据,需要计算数据并按图片输入表格。

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_raspent_time->task_category 作为数组并将其用作字符串
  • @ImranQamer 那么我应该使用什么? :) 寻求帮助
  • 可以给我看看你的表格数据吗?
  • 同时发布带有行号的完整错误消息和错误的完整查询。
  • @ImranQamer 在表花费时间:id、花费时间、daily_spent_time、百分比、daily_percentage、task_category、reason 和 plan_id 表计划:id、assign、estimate_time、user_story、lateness、index 和 project_id

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


【解决方案1】:

你在下面一行有错误

$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'(一个字符串字段)。

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

【讨论】:

  • `$new_spent_time->percentage = $new_spent_time::where('task_category', $spent_time->task_category)->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);'像这样?
  • 不要在哪里使用?只用总和?
  • 但是,如果我不使用 where,那么所有类别都会阅读所有内容,而不是同一个类别
  • 抱歉不能,有没有将数组转换为字符串的解决方案?哦,是的,你为什么不使用teamviewer?
  • 有没有办法把数组转成字符串?
【解决方案2】:

型号

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->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',$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);
    }
}

控制器

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'));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多