查询 Laravel 中特定列的值列表

Query a list of value of a specific column in Laravel

我正在尝试直接从我的数据库的 table 中查询一个列表作为数组,而不必创建另一个 foreach 循环并自己构建一个。


我试试

return Response::json(Skill::select('name')->get());

我明白了

[{"name":"Vagrant"},{"name":"Docker"},{"name":"Gulp"},{"name":"Heroku"},{"name":"RequireJS"},{"name":"AngularJS"},{"name":"Composer "},{"name":"NPM"},{"name":"MySQL"},{"name":"Sublime Text"},{"name":"Laravel"},{"name":"PyCharm"},{"name":"Mac OS X"},{"name":"Windows"},{"name":"Ubuntu"},{"name":"Cent OS"},{"name":"Photoshop"},{"name":"Illustrator"},{"name":"MobaXTerm"},{"name":"Terminal"},{"name":"iMovie"},{"name":"Final Cut"},{"name":"GitHub"},{"name":"BitBucket"},{"name":"Selenium"},{"name":"Python"},{"name":"Bower"},{"name":"Sass"},{"name":"Digital Ocean"},{"name":"Linode"},{"name":"Siteground"},{"name":"Go Daddy"},{"name":"Shopify"},{"name":"Facebook"},{"name":"Twitter"},{"name":"Salesforce"},{"name":"OAuth 2.0"},{"name":"SAML 2.0"},{"name":"OpenID Connect"},{"name":"PostgreSQL"},{"name":"Bash"},{"name":"PHP"},{"name":"Google Map"},{"name":"Google Translation"},{"name":"Instagram"},{"name":"LESS"},{"name":"Geolocation API"},{"name":"Xcode"},{"name":"Atom"},{"name":"Webpack"},{"name":"AWS Console"},{"name":"Secure Shell"},{"name":"Node"},{"name":"Yarn"},{"name":"Pod"},{"name":"EC2"},{"name":"Amazon ECS"},{"name":"S3"},{"name":"Amazon RDS"},{"name":"Camtasia"},{"name":"Core Data"},{"name":"Realm"},{"name":"VS Code"},{"name":"TextMate"},{"name":"TextWrangler"},{"name":"Laravel Elixir"},{"name":"Virtual Machine"},{"name":"Open  Stack"},{"name":"Redis"},{"name":"Local Storage"},{"name":"Protractor"},{"name":"Jest"},{"name":"Mocha"},{"name":"Chai"},{"name":"SinonJS"},{"name":"AWS"},{"name":"HTML"},{"name":"CSS"},{"name":"Javascript"},{"name":"Sketch"},{"name":"iOS"},{"name":"Express"},{"name":"Angular"},{"name":"React Native"},{"name":"jQuery"},{"name":"Nginx"},{"name":"Apache"},{"name":"PayPal"},{"name":"Square "},{"name":"Disqus"},{"name":"YouTube"},{"name":"Swagger"},{"name":"GitLab"},{"name":"Amazon ECR "},{"name":"Jira"},{"name":"Trello "},{"name":"Evernote "},{"name":"Confluence "},{"name":"Word"},{"name":"CodeBox"},{"name":"Markdown"},{"name":"Noteability"},{"name":"Kamar"},{"name":"Jasmine"},{"name":"Swift"},{"name":"Coda"},{"name":"Postman"},{"name":"Wireshark"},{"name":"Transmit"},{"name":"WinSCP"},{"name":"Navicat Premium"},{"name":"Kaleidoscope"},{"name":"Mind Note "},{"name":"Divvy"},{"name":"Duet"},{"name":"Draw.io"},{"name":"Google Draw"},{"name":"VMWare Fusion "},{"name":"Virtualbox"},{"name":"QuickBooks"},{"name":"Chat.io"},{"name":"FusionCharts"},{"name":"Google Chart"},{"name":"J Player"},{"name":"CKEditor"}]

我想得到这些

["Vagrant","Docker","Gulp","Heroku","RequireJS","AngularJS","Composer ","NPM","MySQL","Sublime Text","Laravel","PyCharm","Mac OS X","Windows","Ubuntu","Cent OS","Photoshop","Illustrator","MobaXTerm","Terminal","iMovie","Final Cut","GitHub","BitBucket","Selenium","Python","Bower","Sass","Digital Ocean","Linode","Siteground","Go Daddy","Shopify","Facebook","Twitter","Salesforce","OAuth 2.0","SAML 2.0","OpenID Connect","PostgreSQL","Bash","PHP","Google Map","Google Translation","Instagram","LESS","Geolocation API","Xcode","Atom","Webpack","AWS Console","Secure Shell","Node","Yarn","Pod","EC2","Amazon ECS","S3","Amazon RDS","Camtasia","Core Data","Realm","VS Code","TextMate","TextWrangler","Laravel Elixir","Virtual Machine","Open  Stack","Redis","LocalStorage","Protractor","Jest","Mocha","Chai","SinonJS","AWS","HTML","CSS","Javascript","Sketch","iOS","Express","Angular","React Native","jQuery","Nginx","Apache","PayPal","Square ","Disqus","YouTube","Swagger","GitLab","Amazon ECR ","Jira","Trello ","Evernote ","Confluence ","Word","CodeBox","Markdown","Noteability","Kamar","Jasmine","Swift","Coda","Postman","Wireshark","Transmit","WinSCP","Navicat Premium","Kaleidoscope","Mind Note ","Divvy","Duet","Draw.io","Google Draw","VMWare Fusion ","Virtualbox","QuickBooks","Chat.io","FusionCharts","Google Chart","J Player","CKEditor"]

如何进一步调试它?

明白了..

return Skill::all()->pluck('name');

好像return是我要找的。

[
    "Vagrant",
    "Docker",
    "Gulp",
    "Heroku",
    "RequireJS",
    "AngularJS",
    "Composer ",
    "NPM",
    "MySQL",
    "Sublime Text",
    "Laravel",
    "PyCharm",
    "Mac OS X",
    "Windows",
    "Ubuntu",
    "Cent OS",
    "Photoshop",
    "Illustrator",
    "MobaXTerm",
    "Terminal",
    "iMovie",
    "Final Cut",
    "GitHub",
    "BitBucket",
    "Selenium",
    "Python",
    "Bower",
    "Sass",
    "Digital Ocean",
    "Linode",
    "Siteground",
    "Go Daddy",
    "Shopify",
    "Facebook",
    "Twitter",
    "Salesforce",
    "OAuth 2.0",
    "SAML 2.0",
    "OpenID Connect",
    "PostgreSQL",
    "Bash",
    "PHP",
    "Google Map",
    "Google Translation",
    "Instagram",
    "LESS",
    "Geolocation API",
    "Xcode",
    "Atom",
    "Webpack",
    "AWS Console",
    "Secure Shell",
    "Node",
    "Yarn",
    "Pod",
    "EC2",
    "Amazon ECS",
    "S3",
    "Amazon RDS",
    "Camtasia",
    "Core Data",
    "Realm",
    "VS Code",
    "TextMate",
    "TextWrangler",
    "Laravel Elixir",
    "Virtual Machine",
    "Open  Stack",
    "Redis",
    "Local Storage",
    "Protractor",
    "Jest",
    "Mocha",
    "Chai",
    "SinonJS",
    "AWS",
    "HTML",
    "CSS",
    "Javascript",
    "Sketch",
    "iOS",
    "Express",
    "Angular",
    "React Native",
    "jQuery",
    "Nginx",
    "Apache",
    "PayPal",
    "Square ",
    "Disqus",
    "YouTube",
    "Swagger",
    "GitLab",
    "Amazon ECR ",
    "Jira",
    "Trello ",
    "Evernote ",
    "Confluence ",
    "Word",
    "CodeBox",
    "Markdown",
    "Noteability",
    "Kamar",
    "Jasmine",
    "Swift",
    "Coda",
    "Postman",
    "Wireshark",
    "Transmit",
    "WinSCP",
    "Navicat Premium",
    "Kaleidoscope",
    "Mind Note ",
    "Divvy",
    "Duet",
    "Draw.io",
    "Google Draw",
    "VMWare Fusion ",
    "Virtualbox",
    "QuickBooks",
    "Chat.io",
    "FusionCharts",
    "Google Chart",
    "J Player",
    "CKEditor"
]