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

Laravel插入操作报错:Response::setStatusCode()参数类型错误

Laravel插入操作触发TypeError错误排查

错误信息

TypeError In Response.php line 466 :
Symfony\Component\HttpFoundation\Response::setStatusCode(): Argument #1 ($code) must be of type int, string given, called in /Users/me/Talabv2/dbf.dbestech.com/vendor/laravel/framework/src/Illuminate/Http/Response.php on line 39

相关代码

namespace Illuminate\Http;

use ArrayObject;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use JsonSerializable;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

class Response extends SymfonyResponse
{
    use ResponseTrait, Macroable {
        Macroable::__call as macroCall;
    }

    /**
     * Create a new HTTP response.
     *
     * @param  mixed  $content
     * @param  int  $status
     * @param  array  $headers
     * @return void
     *
     * @throws \InvalidArgumentException
     */
    public function __construct($content = '', $status = 200, array $headers = [])
    {
        $this->headers = new ResponseHeaderBag($headers);

        $this->setContent($content);

        $this->setStatusCode($status);
        $this->setProtocolVersion('1.0');
    }

    /**
     * Set the content on the response.
     *
     * @param  mixed  $content
     * @return $this
     *
     * @throws \InvalidArgumentException
     */
    public function setContent($content)
    {
        $this->original = $content;

        // If the content is "JSONable" we will set the appropriate header and convert
        // the content to JSON. This is useful when returning something like models
        // from routes that will be automatically transformed to their JSON form.
        if ($this->shouldBeJson($content)) {
            $this->header('Content-Type', 'application/json');

            $content = $this->morphToJson($content);

            if ($content === false) {
                throw new InvalidArgumentException(json_last_error_msg());
            }
        }

        // If this content implements the "Renderable" interface then we will call the
        // render method on the object so we will avoid any "__toString" exceptions
        // that might be thrown and have their errors obscured by PHP's handling.
        elseif ($content instanceof Renderable) {
            $content = $content->render();
        }

        parent::setContent($content);

        return $this;
    }

    /**
     * Determine if the given content should be turned into JSON.
     *
     * @param  mixed  $content
     * @return bool
     */
    protected function shouldBeJson($content)
    {
        return $content instanceof Arrayable ||
               $content instanceof Jsonable ||
               $content instanceof ArrayObject ||
               $content instanceof JsonSerializable ||
               is_array($content);
    }

    /**
     * Morph the given content into JSON.
     *
     * @param  mixed  $content
     * @return string
     */
    protected function morphToJson($content)
    {
        if ($content instanceof Jsonable) {
            return $content->toJson();
        } elseif ($content instanceof Arrayable) {
            return json_encode($content->toArray());
        }

        return json_encode($content);
    }
}

问题原因与解决方案

核心原因

错误本质是创建Response实例时传入了字符串类型的HTTP状态码,而Symfony底层的setStatusCode方法要求状态码必须是整数类型。Laravel的Response构造函数第39行调用该方法时,传递的$status参数不符合类型要求。

排查与修复步骤

  1. 检查所有返回响应的代码:
    查找控制器、路由、错误处理逻辑中类似return response($data, '200')或return response()->json($result, '400')的写法,去掉状态码的引号,改为整数形式(如200、400)。

    • 错误示例:
      return response('插入数据失败', '500');
      
    • 正确示例:
      return response('插入数据失败', 500);
      
  2. 确认状态码变量类型:
    如果状态码是通过变量传递的(如$statusCode = '404'),确保变量被转换为整数,可使用(int)$statusCode强制转换类型后再传入响应方法。

  3. 排查插入操作后的响应逻辑:
    重点检查插入操作完成后返回响应的代码分支,尤其是异常捕获块中是否错误地将字符串作为状态码返回。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 02:45:53