我在对象中有发送数组 如何从刀片文件中的数组中获取一些数据
控制器
public function index(Request $request)
{
$projects = Project::get();
return view('projects.index', compact('projects'));
}
刀片文件
@foreach($projects as $project)
<tr>
<td>{{ $project->name }}</td>
<td>{{ $project->servers }}</td>
</tr>
@endforeach
保存在数据库中的编码数组中的服务器,其中只应列出我要列出的名称
我在服务器数组中有数据 dd(json_decode($ project-> servers));
{#1232 ▼
+"id": 494506
+"identifier": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
+"name": "test"
+"protocol_type": "ftp"
+"server_path": "htdocs"
+"auto_deploy_url": "https://adamw.deployhq.com/deploy/codebase-project/to/test/xxxxxxxxxxx"
+"last_revision": "abcdef123456"
+"preferred_branch": "master"
+"branch": null
+"notify_email": null
+"server_group_identifier": null
+"hostname": "ftp.test.com"
+"username": "test"
+"port": 21
+"passive": true
+"force_hidden_files": false
}
答案 0 :(得分:0)
您可以告诉模型将该字段转换为一个数组,该数组将为您解码JSON:
protected $casts = [
'servers' => 'array',
];
尽管如果此变量不会容纳多个服务器,则应将其命名为“服务器”而不是“服务器”。
Laravel 7.x Docs - Eloquent - Mutators - Array and JSON Casting