将在Laravel中检索到的单个SQL值转换为字符串。

huangapple go评论61阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年7月20日 20:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定