Ошибка: попытка получить свойство необъекта при создании или сохранении данных - PullRequest
0 голосов
/ 19 октября 2018

enter image description here

"Попытка получить свойство необъекта"

Модель

При создании новогоданные и хотите сохранить, возникает ошибка, как указано выше

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

Ответы [ 2 ]

0 голосов
/ 19 октября 2018

должно быть при сохранении созданных данных перейдет в форму и подсчитает объем данных

enter image description here

0 голосов
/ 19 октября 2018

"Trying to get property of non-object" означает, что свойство не существует

измените find на findOrFail

$new_spent_time = SpentTime::findOrFail($plan_id);

, laravel вернет ошибку, если ненайти запись в базе данных.

другой способ - проверить, успешно ли выполнен ваш запрос:

$new_spent_time = SpentTime::find($plan_id);
if($new_spent_time){

    $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',$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);
}else{
    return 'no result found';
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...