英文:
Converting a single retrieved sql value to string in laravel
问题
$client_name = DB::table('utility_data')
->select('client_name')
->where('identity_number', '=', $identity_number)
->first();
dd($client_name);
上面的脚本从数据库中选择客户姓名,我想将该值转换为字符串,有关如何实现这一点的任何想法吗?这是我的结果,我想提取值 "Nosi Chefane":
英文:
I am new in laravel, I want to convert SQL result to string in laravel, below is what I did even though I dont get the result a desire, that is returning client_name as a string.
$client_name = DB::table('utility_data')
->select('client_name')
->where('identity_number', '=', $identity_number)
->first();
dd($client_name);
Above script select client name from the database, I want to convert that value to a string, any idea on how to achieve this? This is what I get as result,
I want to extract the value "Nosi Chefane":
{#303 ▼
+"client_name": "Nosi Chefane"
}
答案1
得分: 0
如果您从控制器中打印它,请使用 return $client_name
,您应该会看到数据以JSON对象的形式显示。
英文:
If you printing it from a controller, just use return $client_name
and you should see the data as a JSON object
答案2
得分: 0
只返回翻译好的部分:
如果您只想返回名称本身,您需要在查询返回的对象内访问它:
dd($client_name->client_name);
英文:
If you'd like to return just the name itself, you'd have to access it within the object returned in the query:
dd($client_name->client_name);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论