You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Blade三元表达式数据展示异常问题求助

Laravel Blade三元表达式变量解析异常原因及解决

问题原因

Blade模板引擎对字符串内的变量解析有明确规则:当字符串中包含->的对象属性链时,必须用大括号{}包裹完整的属性访问表达式。你原代码里的href=tel:$item->User->mobile,Blade仅识别并解析了$item->User部分,将User对象转为JSON字符串输出,而后面的->mobile被当作普通文本直接保留,最终导致href属性内容异常。

修正代码

方法一:用大括号包裹完整属性链

<a {{ $item->User->mobile ? "href=tel:{$item->User->mobile} class=underline" : "" }}>{{ $item->User->mobile ?? "" }}</a>

方法二:改用Blade条件语法(更易读)

<a @if($item->User->mobile) href="tel:{{ $item->User->mobile }}" class="underline" @endif>{{ $item->User->mobile ?? "" }}</a>

内容的提问来源于stack exchange,提问作者Jes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:40:58