Response 内容必须是字符串或对象实现 __toString(), "boolean" given after move to psql

The Response content must be a string or object implementing __toString(), "boolean" given after move to psql

一旦我将 Laravel 应用程序从 MySQL 移动到 pSQL。我一直收到这个错误。

The Response content must be a string or object implementing __toString(), "boolean" given.

我有一个 API 应该 return 我的晋升

http://localhost:8888/api/promotion/1

public function id($id){
    $promotion = Promotion::find($id);
    dd($promotion); //I got something here
    return $promotion;
}

它曾经 return 我的推广,现在它 return 一个错误。


dd($促销);

I got 

Promotion {#410 ▼
  #table: "promotions"
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:16 [▼
    "id" => 1
    "cpe_mac" => "000D6721A5EE"
    "name" => "qwrqwer"
    "type" => "img_path"
    "status" => "Active"
    "heading_text" => "qwerq"
    "body_text" => "werqwerqw"
    "img" => stream resource @244 ▶}
    "img_path" => "/images/promotion/1/promotion.png"
    "video_url" => ""
    "video_path" => ""
    "account_id" => 1001
    "img_url" => ""
    "footer_text" => "qwerqwerre"
    "created_at" => "2016-08-04 10:53:57"
    "updated_at" => "2016-08-04 10:53:59"
  ]
  #original: array:16 [▶]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #fillable: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

内容

__ 对此的任何提示/建议将是一个巨大的帮助!

您的回复必须 return 某种 Response 对象。你不能只是 return 一个对象。

因此将其更改为:

return Response::json($promotion);

或者我最喜欢的辅助函数:

return response()->json($promotion);

如果 return 无法响应,则可能是某种编码问题。参见这篇文章:The Response content must be a string or object implementing __toString(), \"boolean\" given."

TL;DR

只是 returning response()->json($promotion) 不会解决这个问题中的问题。 $promotion 是一个 Eloquent 对象,Laravel 将自动 json_encode 进行响应。 json 编码失败,因为 img 属性 是 PHP 流资源,无法编码。

详情

无论您从控制器 return 做什么,Laravel 都将尝试转换为字符串。当您 return 一个对象时,将调用该对象的 __toString() 魔法方法来进行转换。

因此,当您只是 return $promotion 来自您的控制器操作时,Laravel 将对其调用 __toString() 以将其转换为要显示的字符串。

Model 上,__toString() 调用 toJson(),return 是 json_encode 的结果。因此,json_encode 是 returning false,这意味着它是 运行 错误。

您的 dd 表明您的 img 属性是 stream resourcejson_encode 无法编码 resource,因此这可能是导致失败的原因。您应该将 img 属性添加到 $hidden 属性 以将其从 json_encode.

中删除
class Promotion extends Model
{
    protected $hidden = ['img'];

    // rest of class
}

我在使用 ajax 调用从数据库中检索数据时遇到了这个问题。当控制器返回数组时,它将其转换为布尔值。问题是我有 "invalid characters" 像 ú(带口音的 u)。

在导致错误的文件中没有直接指出。但它实际上是在控制器文件中触发的。当控制器文件中定义的方法的 return 值设置为布尔值时,就会发生这种情况。它不能设置为布尔类型,但另一方面,它必须设置或赋予字符串类型的值。可以显示如下:

public function saveFormSummary(Request $request) {
      ... 
      $status = true;
      return $status;
}

Given the return value of a boolean type above in a method, to be able to solve the problem to handle the error specified. Just change the type of the return value into a string type

如下:

public function saveFormSummary(Request $request) {
      ... 
      $status = "true";
      return $status;
}

所以,与其先 return 整个 object,不如将其包装到 json_encode,然后 return。这将 return 一个合适且有效的对象。

public function id($id){
    $promotion = Promotion::find($id);
    return json_encode($promotion);
}

或者,对于 DB,这就像,

public function id($id){
    $promotion = DB::table('promotions')->first();
    return json_encode($promotion);
}

我认为它可能对其他人有帮助。

您可以使用 json_decode(Your variable Name):

json_decode($result)

我从 Model.where 中获取价值,其中一列具有这样的价值

{"dayList":[
            {"day":[1,2,3,4],"time":[{"in_time":"10:00"},{"late_time":"15:00"},{"out_time":"16:15"}]
             },
             {"day":[5,6,7],"time":[{"in_time":"10:00"},{"late_time":"15:00"},{"out_time":"16:15"}]}
           ]
}

因此访问此值表单模型。你必须使用这个代码。

$dayTimeListObject = json_decode($settingAttendance->bio_attendance_day_time,1);

 foreach ( $dayTimeListObject['dayList'] as $dayListArr)
 {
     foreach ( $dayListArr['day'] as $dayIndex)
     {
         if( $dayIndex == Date('w',strtotime('2020-02-11')))
         {
             $dayTimeList= $dayListArr['time'];
         }
     }
 }

 return $dayTimeList[2]['out_time'] ;

您还可以在您的模型文件中定义种姓

 protected $casts = [
    'your-column-name' => 'json'
  ];

所以在这之后就不需要这一行了。

$dayTimeListObject = json_decode($settingAttendance->bio_attendance_day_time,1);

您可以直接访问此代码。

$settingAttendance->bio_attendance_day_time