未定义索引:在laravel 4中

未定义索引:在laravel 4中,laravel,post,laravel-4,Laravel,Post,Laravel 4,这是我的代码。希望你们能帮我,因为我想得到country['CC_FIPS']的值,用在我城市的where语句中 $local = Input::get('locality'); $country = Input::get('country'); $resultCountry= Country::where('COUNTRY_NAME', '=', $country)->get(); //echo $resultCountry; $resultCi

这是我的代码。希望你们能帮我,因为我想得到country['CC_FIPS']的值,用在我城市的where语句中

    $local = Input::get('locality');
    $country = Input::get('country');
    $resultCountry= Country::where('COUNTRY_NAME', '=', $country)->get();

    //echo $resultCountry;
    $resultCity= City::where('FULL_NAME_ND', '=', $local)
            ->where('CC_FIPS', '=', $resultCountry['CC_FIPS'])
                ->get();
        }

    echo $resultCity;

问题是数组$resultCountry没有任何索引为CC_FIPS的元素

如果将函数dd()用作


您可以将$resultCountry转换为如下数组:

$resultCountry= Country::where('COUNTRY_NAME', '=', $country)->get()->toArray();
那么$resultCountry['CC_FIPS']应该正确地返回您的值

 $local = Input::get('locality');
    $country = Input::get('country');
    $resultCountry= Country::where('COUNTRY_NAME', '=', $country)->first()->toArray();

    //echo $resultCountry;
    $resultCity= City::where('FULL_NAME_ND', '=', $local)
            ->where('CC_FIPS', '=', $resultCountry['CC_FIPS'])
                ->get();
        }

    print_r($resultCity);
而不是先使用,然后保持代码的其余部分t=不变。。希望这有帮助

 $local = Input::get('locality');
    $country = Input::get('country');
    $resultCountry= Country::where('COUNTRY_NAME', '=', $country)->first();

    //echo $resultCountry;
    $resultCity= City::where('FULL_NAME_ND', '=', $local)
            ->where('CC_FIPS', '=', $resultCountry['CC_FIPS'])
                ->get();
        }

    print_r($resultCity);
在第一次查询中使用Get时,结果将返回如下内容

[
   [0]=>[
        ]
]
因此,与其先获取使用,不如先获取一个数组
另外,在第二个查询中,您无法回显您需要打印的结果

@谢谢margus爵士提供的信息,但我已经使用foreach解决了此问题:)太好了!如果你找到了解决方案,那么你可以接受答案来结束问题。
[
   [0]=>[
        ]
]