如果我这样做的话,我正试图从blockid
中获取blocksobject
:
return $blocksobject
我的输出是:
[{"userid":5,"objectid":25,"id":25,"blockid":3},
{"userid":5,"objectid":26,"id":26,"blockid":3},
{"userid":5,"objectid":28,"id":28,"blockid":4}]
代码:
$blocksobject = ObjectAccess::select('objectsaccess.userid', 'objectsaccess.objectid', 'objects.id', 'objects.blockid')
->where('objectsaccess.userid', Auth::user()->id)
->join('objects', 'objects.id', '=', 'objectsaccess.objectid')
->get();
但如果我:
return $blocksobject->blockid;
我收到以下错误:
此集合实例上不存在属性[objectid]。
知道为什么会这样吗?
谢谢!
EDIT1:BLADE
@foreach($blocksobject as $blockk->where($block->id,'=','objects.blockid')->first())
$blockids[] = $block->blockid;
@endforeach
$ block-> id在刀片中工作,它会输出当前的ID,所以我只想获取与当前ID匹配的项目。 给出错误:不能在写上下文中使用方法返回值
答案 0 :(得分:0)
这里的问题是你有多个块ID。
如果您想单独获得每个结果,您可以执行以下操作,这将返回您的块数组
$blocksobject = ObjectAccess::select('objectsaccess.userid', 'objectsaccess.objectid', 'objects.id', 'objects.blockid')
->where('objectsaccess.userid', Auth::user()->id)
->join('objects', 'objects.id', '=', 'objectsaccess.objectid')
->get();
$blockids = [];
foreach($blocksobject as $block) {
$blockids[] = $block->blockid;
}
return $blockids;
如果您想在查询中返回第一个结果,可以执行以下操作
$blocksobject = ObjectAccess::select('objectsaccess.userid', 'objectsaccess.objectid', 'objects.id', 'objects.blockid')
->where('objectsaccess.userid', Auth::user()->id)
->join('objects', 'objects.id', '=', 'objectsaccess.objectid')
->first();
return $blocksobject;
第一种方法将返回一个对象,而get方法返回结果集合
答案 1 :(得分:0)
这将为当前用户提供objects.blockid = 3
。
$blocksobject = ObjectAccess::select('objectsaccess.userid', 'objectsaccess.objectid', 'objects.id', 'objects.blockid')
->where(['objectsaccess.userid' => Auth::user()->id , 'objects.blockid' => '3' ])
->join('objects', 'objects.id', '=', 'objectsaccess.objectid')
->get();
如果你返回$blocksobject->blockid;
,毫无疑问它会给你一个错误,因为$blocksobject
是一个多维数组而不是数组。
您应该返回此内容:return $blocksobject;
并处理您希望在视图中的foreach循环中显示的内容。
例如,获取每条记录的userid和blockid:
@foreach($blocksobject as $singleobject)
<div> {{ $singleobject->userid; }}</div>
<div> {{ $singleobject->blockid; }}</div>
@endforeach