错误:创建或保存数据时试图获取非对象的属性
Posted
技术标签:
【中文标题】错误:创建或保存数据时试图获取非对象的属性【英文标题】:Error: Trying to get property of non-object when creating or saving data 【发布时间】:2019-03-23 21:50:26 【问题描述】:"试图获取非对象的属性"
型号
新建数据要保存时,出现如上错误
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);
【问题讨论】:
【参考方案1】:"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';
【讨论】:
但是,找不到页面。还有其他解决方案吗? 保存时创建具有相同类别的新数据。 这意味着 laravel 在你的数据库中没有找到任何记录。尝试返回$plan_id
并查看它返回的数据,然后在您的数据库中比较它,如果 id
确实存在。
如果使用 $new_spent_time = SpentTime::findorfail($plan_id);,则找不到页面。但如果使用 $new_spent_time = SpentTime::get();像这样的错误“此集合实例上不存在属性 [task_category]。”
Route::resource('/real', 'TrackerController');【参考方案2】:
应该是在存储的时候创建的数据会去表单计算数据量
【讨论】:
以上是关于错误:创建或保存数据时试图获取非对象的属性的主要内容,如果未能解决你的问题,请参考以下文章