我遇到了以下错误
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/