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

Laravel Blade中如何去除数组格式,直接获取字段值?

问题描述

我想在当前表格中调用另一张表的字段展示,使用的查询代码如下:

DB::table('tbl_users')
->where('id', $complaints->user_id)
->limit(1)
->get('phone');

当前输出为:

[{"phone":"0123456789"}]

我希望实现的输出是:

0123456789

尝试过编码/解码以及用JSON包裹整个查询,但都没有效果。

更新内容

控制器代码

public function index()
{
    $complaint = DB::table('complaint')->get();
    $phone = DB::table('tbl_users')
->where('id', $complaint->user_id)
->select('phone')
->first();

    return view('admin.complaints',compact('complaint','phone'));
}

Blade视图代码

@foreach ($complaint as $complaints)
     <tr>
     <th style="padding: 20px">{{ $complaints->id }}</th>
     <th style="padding: 20px">{{ $complaints->createdDate }}</th>
     <th style="padding: 20px">New</th>
     <th style="padding: 20px">{{ $complaints->user_id }}</th>
     <th style="padding: 20px">{{$phone->phone}}</th>
     </tr>
@endforeach
解决方案

1. 原始查询的问题

你用get()方法会返回一个集合(Collection),哪怕只查一条数据,也是包含单个对象的集合,所以输出是数组格式。要直接拿到phone字段值,最简洁的写法是用value()方法:

DB::table('tbl_users')
->where('id', $complaints->user_id)
->value('phone');

或者用first()获取单个对象后再取字段:

DB::table('tbl_users')
->where('id', $complaints->user_id)
->first()->phone;

2. 控制器与视图的逻辑错误

你当前的控制器代码有两个问题:

  • $complaint = DB::table('complaint')->get()返回的是所有投诉的集合,集合没有user_id属性,只有单个投诉对象才有,所以$complaint->user_id是无效的。
  • 提前查一次$phone只能拿到一个用户的手机号,循环渲染时所有行都会显示同一个手机号,不符合需求。

推荐两种修正方案:

方案一:循环内查询(适合小数据量场景)

修改控制器,只传递投诉集合:

public function index()
{
    $complaints = DB::table('complaint')->get();
    return view('admin.complaints', compact('complaints'));
}

在Blade视图的循环中,针对每个投诉查询对应用户的手机号:

@foreach ($complaints as $complaint)
     <tr>
     <th style="padding: 20px">{{ $complaint->id }}</th>
     <th style="padding: 20px">{{ $complaint->createdDate }}</th>
     <th style="padding: 20px">New</th>
     <th style="padding: 20px">{{ $complaint->user_id }}</th>
     <th style="padding: 20px">
         {{ DB::table('tbl_users')->where('id', $complaint->user_id)->value('phone') }}
     </th>
     </tr>
@endforeach

方案二:关联查询(推荐,避免N+1查询性能问题)

如果你的模型定义了关联关系,用预加载关联的方式可以一次性查询所有需要的数据,性能更优:

首先在Complaint模型中定义与User的关联:

public function user()
{
    return $this->belongsTo(User::class, 'user_id');
}

然后控制器中用with()预加载关联数据:

public function index()
{
    $complaints = Complaint::with('user')->get();
    return view('admin.complaints', compact('complaints'));
}

最后在视图中直接调用关联字段,同时添加容错处理避免用户不存在时报错:

@foreach ($complaints as $complaint)
     <tr>
     <th style="padding: 20px">{{ $complaint->id }}</th>
     <th style="padding: 20px">{{ $complaint->createdDate }}</th>
     <th style="padding: 20px">New</th>
     <th style="padding: 20px">{{ $complaint->user_id }}</th>
     <th style="padding: 20px">{{ $complaint->user->phone ?? '无手机号' }}</th>
     </tr>
@endforeach

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 11:18:43