简单的代码不起作用!我一直在使用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 = $ 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))
修改 我删除了我遇到错误的代码的第一部分,然后得到了这个,同样的事情的另一个例子,但使用一个较小的对象,这一行比上一个更重要。所有细节都已更新。
答案 0 :(得分:1)
最好检查isset
public function isAllowed($perm)
{
$cando = 0;
$groups = DB::table('group_user')->where('user_id', $this->id)->get();
if(isset($groups)&&count($groups)>0){
foreach ($groups as $mygroup)
{
if(isset($mygroup->group_id)){
$group = Group::find($mygroup->group_id);
}
if (!is_null($group->project_id))
{
continue;
}
}
}
}
答案 1 :(得分:0)
您尝试访问的属性是数组“属性”的元素,因此请尝试:
public function isAllowed($perm)
{
$cando = 0;
$groups = DB::table('group_user')->where('user_id', $this->id)->get();
foreach ($groups as $mygroup)
{
$group = Group::find($mygroup->group_id);
if (!is_null($group->attributes['project_id']))
{
continue;
}
}
}
希望这会有所帮助:)
答案 2 :(得分:0)
首先,确保在组模型中将“project_id”添加到public function isAllowed($perm)
{
$cando = 0;
$groups = DB::table('group_user')->where('user_id', $this->id)->get();
foreach ($groups as $mygroup)
{
$group = Group::whereId($mygroup->group_id)->first();
if (!is_null($group->attributes['project_id']))
{
continue;
}
}
}
数组中
试试这个
<ListView x:Name="myListView" ItemsSource="{Binding players}" HorizontalOptions="FillAndExpand" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
...
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Text="{Binding Status, Converter={StaticResource StatusToTextConverter}, Mode=TwoWay}" HorizontalOptions="CenterAndExpand"/>
<Button Grid.Column="1" Text="{Binding Status, Converter={StaticResource StatusToTextExConverter}, Mode=TwoWay}" HorizontalOptions="CenterAndExpand"/>
...
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>