2017-07-04 38 views
1

所以我的问题,打印结果在刀片的数据是从控制器,因此:非法串偏移和试图获得非对象的属性(laravel 5.3)

  1. UseController.php:

     $expertCategory = Category::getCategoryByID($user_id); 
        $data = [ 'expertCategory' => $expertCategory ]; 
        return view('cms.user.edit', $data); 
    
  2. Category.php(型号)getCategoryByID($ USER_ID)返回结果数组,如果I DD(expertCategory);在控制器,其结果是:

    array:9 [▼ 
        "id" => 1 
        "name" => "Beauty" 
        "sequence" => 1 
        "background_color" => "ffffff" 
        "status" => "Active" 
        "created_at" => "2017-06-19 09:41:38" 
        "updated_at" => "2017-06-19 09:41:38" 
        "icon_filename" => "beauty-icon" 
        "iconURL" => array:3 [▼ 
          "small" => "http://localhost:8000/images/category_icons/small/beauty-icon" 
          "medium" => "http://localhost:8000/images/category_icons/medium/beauty-icon" 
        ] 
    ] 
    
  3. 但是,当我想用​​的foreach的结果blade.php与代码打印:

    @foreach($expertCategory as $expertCat) 
        {{ $expertCat->id }} 
    @endforeach 
    

将返回错误“试图让非对象“

的属性如果我使用这样的代码:

@foreach($expertCategory as $expertCat) {{ $expertCat['id'] }} @endforeach

则回复:“非法串偏移‘身份证’”

有人能帮助解决这个问题:■?非常感谢 !

+0

不要做foreach,它只是'$ expertCategory [“id”]'。毕竟这是一个单一的类别。 – apokryfos

+0

[PHP:“注意:未定义的变量”,“注意:未定义的索引”和“注意:未定义的偏移量”的可能重复](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable- notice-undefined-index-and-notice-undef) –

+0

谢谢先生,这真的很有用,并且只是意识到我的查询使用“first()”,这就是为什么每次使用时都会出错。感谢你的帮助!上帝保佑! – Axel

回答

0

由于$expertCategory是一个dimensional array你正面临这个问题

只需更换这

$expertCategory = Category::getCategoryByID($user_id); 
$data = [ 'expertCategory' => $expertCategory ]; 
return view('cms.user.edit', $data); 

随着

$expertCategory = Category::getCategoryByID($user_id); 
$data = [ 'expertCategory' => [$expertCategory] ]; 
return view('cms.user.edit', $data); 

然后使用

@foreach($expertCategory as $expertCat) 
    {{ $expertCat['id'] }} 
@endforeach 

在你的刀片中它会为你工作。

+0

谢谢先生Sahoo,只是意识到我的查询选择“first()”只有这就是为什么我不能用于每个。谢谢你的帮助先生! – Axel

+0

@Axel欢迎您,如果您的问题解决了,只需通过接受我的答案来关闭您的问题。谢谢:) –

+0

好吧,先生,完成,非常感谢:D – Axel

相关问题