久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

響應(yīng)內(nèi)容必須是實現(xiàn) __toString()、“boolean"和“

The Response content must be a string or object implementing __toString(), quot;booleanquot; given after move to psql(響應(yīng)內(nèi)容必須是實現(xiàn) __toString()、“boolean和“boolean的字符串或?qū)ο?移動到 psql 后給出) - IT屋-程
本文介紹了響應(yīng)內(nèi)容必須是實現(xiàn) __toString()、“boolean"和“boolean"的字符串或?qū)ο?移動到 psql 后給出的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

一旦我將我的 Laravel 應(yīng)用程序從 MySQL 移動到 pSQL.我一直收到這個錯誤.

<塊引用>

響應(yīng)內(nèi)容必須是一個字符串或?qū)ο螅瑢崿F(xiàn)了 __toString(),給出了boolean".

我有一個 API 可以返回我的促銷

__對此的任何提示/建議都將是一個巨大的幫助!

解決方案

TL;DR

僅返回 response()->json($promotion) 并不能解決此問題中的問題.$promotion 是一個 Eloquent 對象,Laravel 會自動為響應(yīng)進(jìn)行 json_encode.json 編碼失敗,因為 img 屬性是 PHP 流資源,無法編碼.

詳情

無論你從控制器返回什么,Laravel 都會嘗試轉(zhuǎn)換為字符串.當(dāng)您返回一個對象時,將調(diào)用該對象的 __toString() 魔術(shù)方法進(jìn)行轉(zhuǎn)換.

因此,當(dāng)您從控制器操作中返回 $promotion 時,Laravel 將對其調(diào)用 __toString() 以將其轉(zhuǎn)換為要顯示的字符串.

Model上,__toString()調(diào)用toJson(),返回json_encode的結(jié)果.因此,json_encode 返回 false,這意味著它遇到了錯誤.

您的 dd 表明您的 img 屬性是一個 stream 資源.json_encode 無法對 resource 進(jìn)行編碼,因此這可能是導(dǎo)致失敗的原因.您應(yīng)該將 img 屬性添加到 $hidden 屬性以將其從 json_encode 中刪除.

class Promotion 擴展模型{受保護的 $hidden = ['img'];//其余課程}

As soon as I move my Laravel App from MySQL to pSQL. I kept getting this error.

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

I have an API that suppose to return my promotion

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

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

It used to return my promotion, now it return an error.


dd($promotion);

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
}


Content

__ Any hints / suggestions on this will be a huge help!

解決方案

TL;DR

Just returning response()->json($promotion) won't solve the issue in this question. $promotion is an Eloquent object, which Laravel will automatically json_encode for the response. The json encoding is failing because of the img property, which is a PHP stream resource, and cannot be encoded.

Details

Whatever you return from your controller, Laravel is going to attempt to convert to a string. When you return an object, the object's __toString() magic method will be invoked to make the conversion.

Therefore, when you just return $promotion from your controller action, Laravel is going to call __toString() on it to convert it to a string to display.

On the Model, __toString() calls toJson(), which returns the result of json_encode. Therefore, json_encode is returning false, meaning it is running into an error.

Your dd shows that your img attribute is a stream resource. json_encode cannot encode a resource, so this is probably causing the failure. You should add your img attribute to the $hidden property to remove it from the json_encode.

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

    // rest of class
}

這篇關(guān)于響應(yīng)內(nèi)容必須是實現(xiàn) __toString()、“boolean"和“boolean"的字符串或?qū)ο?移動到 psql 后給出的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Laravel Eloquent Union query(Laravel Eloquent Union 查詢)
Overwrite laravel 5 helper function(覆蓋 Laravel 5 輔助函數(shù))
laravel querybuilder how to use like in wherein function(laravel querybuilder 如何在 where 函數(shù)中使用 like)
Roles with laravel 5, how to allow only admin access to some root(Laravel 5 的角色,如何只允許管理員訪問某些根)
Laravel Auth - use md5 instead of the integrated Hash::make()(Laravel Auth - 使用 md5 而不是集成的 Hash::make())
How to set and get Cookie in laravel(如何在 Laravel 中設(shè)置和獲取 Cookie)
主站蜘蛛池模板: 国产日韩一区二区三免费高清 | 精品一区二区三区四区外站 | 亚洲成年在线 | 久久久久久电影 | 我爱操 | 午夜视频一区二区三区 | 日韩视频在线观看一区二区 | 日本羞羞影院 | 国产成人精品一区二区三区在线 | 中文字幕一区二区三区在线观看 | 日韩av在线不卡 | 天天干天天插天天 | www.欧美| 精品欧美乱码久久久久久1区2区 | 国产一级淫片a直接免费看 免费a网站 | 蜜桃视频在线观看www社区 | 亚洲欧美激情国产综合久久久 | 久久精品色视频 | 亚洲综合色视频在线观看 | 日韩一二区在线 | 日韩视频中文字幕 | 中文字幕国产精品 | 草樱av | www.se91| 手机在线观看 | 久久91av| 亚洲综合一区二区三区 | 美女久久久久 | 99re在线播放 | 国产精品久久久久久久久久久久冷 | 亚洲一本 | 久久久日韩精品一区二区三区 | 天堂va在线观看 | 99中文字幕| 久久国产精品99久久久久久丝袜 | 成人免费淫片aa视频免费 | 99精品视频免费在线观看 | 9久9久| 亚洲欧美另类在线 | 男人影音 | 国产精品视频一区二区三区 |