php - 未定义的属性:我的代码中的 stdClass::$account_id 错误

标签 php mysql laravel-4

我遇到了以下错误

Undefined property: stdClass::$account_id (View: C:\xampp\htdocs\laravel\awsconfig\app\views\search.blade.php)

这是导致此错误的代码:

$resource_types = array();
$resource_types['AWS::EC2::Instance'] = 'EC2Instance';
$resource_types['AWS::EC2::NetworkInterface'] = 'EC2NetworkInterface';
$resource_types['AWS::EC2::VPC'] = 'VPC';
$resource_types['AWS::EC2::Volume'] = 'Volume';
$resource_types['AWS::EC2::SecurityGroup'] = 'EC2SecurityGroup';
$resource_types['AWS::EC2::Subnet'] = 'Subnet';
$resource_types['AWS::EC2::RouteTable'] = 'RouteTable';
$resource_types['AWS::EC2::EIP'] = 'EIP';
$resource_types['AWS::EC2::NetworkAcl'] = 'NetworkAcl';
$resource_types['AWS::EC2::InternetGateway'] = 'InternetGateway';


 $accounts = DB::table('aws_account')->get();
 $account_list = array();




foreach(glob('../app/views/*.json') as $filename)
{
    //echo $filename;
    $data = file_get_contents($filename);

    if($data!=null)

    {


            $decoded=json_decode($data,true);  


            if(isset($decoded["Message"])) 
            { 
            //echo "found message<br>";
                $message= json_decode($decoded["Message"]);
if(isset($message->configurationItem))
{
//  echo"found cfi<br>";    
            $insert_array = array();
                $cfi = $message->configurationItem;
                switch ($cfi->configurationItemStatus)
                {

                    case "ResourceDiscovered":
                //echo"found Resource Discovered<br>";  
                        if (array_key_exists($cfi->resourceType,$resource_types))

                        {
                            //var_dump($cfi->resourceType);
                            $resource = new $resource_types[$cfi->resourceType];

                            foreach ($cfi->configuration as $key => $value)
                            {
                                if (in_array($key,$resource->fields))
                                {
                                    $insert_array[from_camel_case($key)] = $value;
                                }
                            }




                            $resource->populate($insert_array);
                            if (!$resource->checkExists())
                            {
                                $resource->save();

                                    if(isset($cfi->configuration->tags))
                                    {
                                       foreach ($cfi->configuration->tags as $t )

                                        {

                                        $tag= new Tag;
                                    $tag->resource_type = "instance";

                                    $tag->resource_id = $resource->id;
                                    $tag->key = $t->key;
                                    $tag->value = $t->value;

                                    $tag->save();


                                        if(isset($cfi->awsAccountId))
                                    {
                                     foreach ($accounts as $a)

                                     {
                                       $account_list[] = $a->account_id;
                                     }


                                    if (!in_array($account_id,$account_list))
                                    {



                                    $account_id = new Account;
                                    $account_id->aws_account_id = $cfi->awsAccountId;

                                    $account_list[] = $account_id;
                                     $account_id->save();

                                        }        






                                }
                                }
}




                            }

                        }

                        else
                        {
                            echo "Creating ".$cfi["resourceType"]." not yet supported<br>";
                        }





                    break;

我知道这会很愚蠢,我一如既往地感谢所有帮助

最佳答案

根据您的代码,这是我正在解释的一个简单的演示,DB::select 将返回一个包含多个对象(可能不止一个)的数组,然后将其分配给$this->食物.

记住,$this->food 看起来像

数组( [0] => stdClass 对象 ( [名称] => '牛肉' ) )

实际上,$this->food->name 正在尝试访问未定义的属性。

解决方案1:

您应该使用$this->food[0]->name来访问它。

虽然看起来很奇怪,但确实有效。

解决方案2:

为什么不调用 Food::find($id) 来获取对象而不是 $food = new food($id)

您可以通过阅读此内容了解更多信息http://laravel.com/docs/eloquent

希望这可以帮助您解决问题

关于php - 未定义的属性:我的代码中的 stdClass::$account_id 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29942824/

相关文章:

php - 在 PHP 中获取日期和数字工作日

mysql - 如何导入 SQL 文件 (MySQL)

mysql - 错误 : Duplicate entry '0' for key 'PRIMARY'

php - Laravel,没有 Shell Access 的转储自动加载

amazon-web-services - Laravel 4.2 AWS SQS 队列设置使用 EB 工作环境

php - Mysql触发器从一个表到另一个

javascript - 可通过 Ajax 下载 CSV 文件

mysql - 如何避免nodejs mysql中大量查询出现死锁?

javascript - 在数据表上显示图像

php - 在 MySQL 中存储 0.00001