Laravel试图在对象中获取非对象的属性(Laravel trying to get property of non-object in an object)
简单的代码不起作用! 我一直收到错误尝试使用if语句获取非对象的属性 。 我尝试像数组一样访问它,我收到一条消息,说它无法访问stdClass作为数组。
public function isAllowed($perm) { $cando = 0; $groups = DB::table('group_user')->where('user_id', $this->id)->get(); foreach ($groups as $mygroup) { $group_can = DB::table('group_permission')->where([ ['permission_id', $permission->id], ['group_id', $mygroup->group_id] ])->first(); $setting = $group_can->setting; // Error returns for this line // if ($setting > $cando) { $cando = $setting; } } }
$ group_can的print_r,var_dump和dd给出:
stdClass Object ( [group_id] => 1 [permission_id] => 50 [setting] => 1 ) object(stdClass)#555 (3) { ["group_id"]=> int(1) ["permission_id"]=> int(50) ["setting"]=> int(1) } {#555 ▼ +"group_id": 1 +"permission_id": 50 +"setting": 1 }
使用$ setting = $ group_can-> setting; 返回错误尝试获取非对象的属性
使用$ setting = $ group_can ['setting']; 返回错误不能使用stdClass类型的对象作为数组
laravel错误的细节是:
at HandleExceptions-> handleError(8,'试图获取非对象的属性','/ home / mwsubmissions/public_html/jon/MWSubmissionManager/app/User.php',91,array('perm'=>'manage。 projects','cando'=> 1,'groups'=> object(Collection),'permission'=> object(stdClass),'mygroup'=> object(stdClass),'group'=> null,'group_can' => null,'setting'=> 1))
编辑我删除了我遇到错误的代码的第一部分,然后得到了这个,同样的事情的另一个例子,但使用一个较小的对象,这一行比上一个更重要。 更新所有细节。
Simple code will not work! I keep getting the error Trying to get property of non-object on the line with the if statement. I tried accessing this like an array I get a message saying it can't access a stdClass as an Array.
public function isAllowed($perm) { $cando = 0; $groups = DB::table('group_user')->where('user_id', $this->id)->get(); foreach ($groups as $mygroup) { $group_can = DB::table('group_permission')->where([ ['permission_id', $permission->id], ['group_id', $mygroup->group_id] ])->first(); $setting = $group_can->setting; // Error returns for this line // if ($setting > $cando) { $cando = $setting; } } }
print_r, var_dump, and dd of $group_can give this:
stdClass Object ( [group_id] => 1 [permission_id] => 50 [setting] => 1 ) object(stdClass)#555 (3) { ["group_id"]=> int(1) ["permission_id"]=> int(50) ["setting"]=> int(1) } {#555 ▼ +"group_id": 1 +"permission_id": 50 +"setting": 1 }
Using $setting = $group_can->setting; returns the error Trying to get property of non-object
Using $setting = $group_can['setting']; returns the error Cannot use object of type stdClass as array
The details of the laravel error are:
at HandleExceptions->handleError(8, 'Trying to get property of non-object', '/home/mwsubmissions/public_html/jon/MWSubmissionManager/app/User.php', 91, array('perm' => 'manage.projects', 'cando' => 1, 'groups' => object(Collection), 'permission' => object(stdClass), 'mygroup' => object(stdClass), 'group' => null, 'group_can' => null, 'setting' => 1))
EDIT I removed the first part of the code that I was having errors with and then got to this, another example of the same thing, but using a smaller object and this line is more important than the last was. All details updated.
原文:https://stackoverflow.com/questions/46188186
最满意答案
好吧,我找到了与熊猫的方式。 我已将每个列表分成一个列表,然后传递给像这样的pandas:
excelResult = pd.DataFrame({'Imballo': imballoColumn, 'Data': dateColumn, 'Abs/Empfae': absColumn, 'Quantità in entrata': inColumn, 'Quantità in uscita': outColumn, 'Numero Bolla': ddtColumn, 'WK-LG-LR': wkColumn}) writer = pd.ExcelWriter('theResult.xlsx', engine='xlsxwriter') excelResult.to_excel(writer, sheet_name='mismatching') writer.save()
Ok, I found a way with Pandas. I've divided every list in a single list and then passed to pandas like this:
excelResult = pd.DataFrame({'Imballo': imballoColumn, 'Data': dateColumn, 'Abs/Empfae': absColumn, 'Quantità in entrata': inColumn, 'Quantità in uscita': outColumn, 'Numero Bolla': ddtColumn, 'WK-LG-LR': wkColumn}) writer = pd.ExcelWriter('theResult.xlsx', engine='xlsxwriter') excelResult.to_excel(writer, sheet_name='mismatching') writer.save()
相关问答
更多-
如何将不同大小的列表从python导出到excel(How to export a list with different size from python to excel)[2022-06-29]
如果您可以使用csv模块写入csv,然后在excel中打开它,那么您可以使用csv模块和zip() ,将列表列表写为列。 示例 - import csv with open('','w') as f: writer = csv.writer(f) #writer.writerow([ ]) #uncomment this and put header, if you want header row , if not leave it commen ... -
看到这个链接,它可以帮助你: http : //www.liferay.com/fr/community/forums/-/message_boards/message/17450310 see this link, it could help you : http://www.liferay.com/fr/community/forums/-/message_boards/message/17450310
-
因此,使用fromArray()认真提供的fromArray()方法,允许您在一个调用中从一个数组中写入整行或整个单元块。 查看示例并阅读文档总是有帮助的 附加说明 顺便说一下, $objPHPExcel->setCellValue('C5', $v)只有在$objPHPExcel是一个工作表时才有效,大多数例子都使用$ objPHPExcel作为工作簿 (即工作表的集合),所以不要混淆 编辑 要从数据库中获取结果,请使用 sqlsrv_fetch_array($tsql, SQLSRV_FETCH_ASS ...
-
好吧,我找到了与熊猫的方式。 我已将每个列表分成一个列表,然后传递给像这样的pandas: excelResult = pd.DataFrame({'Imballo': imballoColumn, 'Data': dateColumn, 'Abs/Empfae': absColumn, 'Quantità in entrata': inCo ...
-
为什么不使用exists()方法而不是你正在做什么? 这就是它的目的。 布尔值存在(ID id) 返回具有给定id的实体是否存在。 if(!te.exits(wb.getSheetName(i))){ TableEntity t=new TableEntity(wb.getSheetName(i), ""); te.save(t) } Why not use the exists() method instead of what you are doing? That is what it ...
-
尝试添加这段代码。 我已经测试过了。 它运作良好。 Dim validationRange As Range Sheets("Formularz").Cells(6, 2).Select Set validationRange = Sheets("Dane").Range("P" & i & ":P" & j) With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertSto ...
-
使用jxl api将新列插入到现有的excel文件中(Inserting new column to an already existing excel file using jxl api)[2023-02-15]
可能你使用的api并不是最好的。 我也有这个问题(insertcolumn行的nullpointer异常)并且在我下载jexcelapi之前找不到任何解决方案。 HTH Probably the api you are using is not the best one. I also had this problem (nullpointer exception at the insertcolumn line) and couldnt find any solution until I download ... -
private void FindAndSetDate(WorkSheet ws, Dictionary
dict) { Range find = null; foreach(KeyValuePair kvp in Dict) { find = ws.Cells.Find(kvp.Key, Type.Missing, Microsoft.Office.Interop.Excel.XlF ... -
批处理文件重命名 - 从列表中插入文本(使用Python或Java)(Batch file renaming – inserting text from a list (in Python or Java))[2022-09-06]
如果你有一个以相同顺序生成文件的名称列表,那么在Python中它就像这个未经测试的片段: #!/usr/bin/python import os f = open('list.txt', 'r') for n, name in enumerate(f): original_name = 'BusinessCard_%02d_Blue.pdf' % (n + 1) new_name = 'BusinessCard_%02d_%s_Blue.pdf' % ( ... -
如何将excel文件中的特定数据附加到python列表中?(How to append specific data from excel file into a python list?)[2022-02-07]
请尝试以下方法: import tkFileDialog import xlrd excel_file = tkFileDialog.askopenfilename(filetypes=[('excelfile','*.xlsx')],title='Choose a .xlsx file') workbook = xlrd.open_workbook(excel_file) sheet = workbook.sheet_by_index(0) data = [sheet.row_values(i)[ ...