【问题标题】:Laravel: getting error when trying to return specific orders to a userLaravel:尝试将特定订单返回给用户时出错
【发布时间】:2021-08-06 15:25:08
【问题描述】:
public function user(Request $request, $user_id){

    return Order::where('user_id', '=', `$user_id`);

}

给我以下错误:

参数 1 传递给 Symfony\Component\HttpFoundation\Response::setContent() 必须是 类型 string 或 null,给定对象,调用 /app/vendor/laravel/framework/src/Illuminate/Http/Response.php 上线 72

我希望能够根据作为路由参数传入的用户 ID 将订单返回给用户。

【问题讨论】:

  • 我错过了语句末尾的 ->get()。

标签: laravel


【解决方案1】:

要做的两件事:

  1. 删除$user_id变量周围的``
  2. 在语句末尾添加 ->get() 以获取结果集合。
public function user(Request $request, $user_id){

    return Order::where('user_id', '=', $user_id)->get();

}

如果您正在执行 ajax 请求,则必须返回响应:

public function user(Request $request, $user_id){

    return response()->json(['orders' => Order::where('user_id', '=', $user_id)->get()])

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 2022-11-22
    • 2018-01-05
    • 2018-11-22
    相关资源
    最近更新 更多