我遇到了以下错误
未定义的属性:stdClass::$account_id(视图:
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->food
。
记住,$this->food
看起来像Array ( [0] => stdClass Object ( [name] => 'Beef' ))
实际上,$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/