我将信息输入数据库如下:
public function addFlight(){
Flight::create([
'name' => 'Sidney',
'address' => [
'street' => 'Williams road',
'suburb' => 'Parow'
]
]);
return "Saved";
}
因此,在我的数据库中,地址存储为json,并且我在模型中完成了此操作:
protected $casts = [
'address' => 'array'
];
我现在想要的是每次循环飞行时获取地址信息。此刻,我有了这个信息:
public function getFlight(){
$flights = Flight::all();
foreach ($flights as $flight) {
foreach($flight->address as $address){
echo $address->street;
}
}
}
如何访问地址信息(街道和郊区)?
此刻,如果我尝试通过url访问getFlights方法,则会出现以下错误:
Trying to get property 'street' of non-object