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

将变量传入with()方法遇异常:返回数组而非目标字符串

解决方法:获取单个字段值而非集合输出

你的问题出在$name_fighter的取值方式上:

  • 调用get()方法会返回一个Eloquent集合(本质是包含对象的数组),直接拼接字符串时会输出集合的默认字符串形式,也就是你看到的[{"name":"Ryu"}]。

你可以通过以下几种方式获取单个字段的具体值:

方法1:使用value()直接获取字段值

这是最简洁的方式,直接返回指定字段的单个值:

$name_fighter = DB::table('fighters')->where('id','=',$id)->value('name');

方法2:用first()获取单条记录后取字段

先获取单条记录对象,再访问其name属性:

$name_fighter = DB::table('fighters')->select('name')->where('id','=',$id)->first()->name;

方法3:通过Eloquent模型获取(推荐)

既然你已经定义了FighterModel,用模型操作更符合Laravel的最佳实践,还能避免重复查询数据库:

public function update(FighterRequest $request, $id)
{
    $fighter = FighterModel::findOrFail($id);
    $fighter->update($request->validated());
    return redirect('fighter')->with('success-update',"{$fighter->name} won updates in your informations.");  
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 11:15:33