ihue ihue - 1 year ago 380
MySQL Question

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

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


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.


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

Details of my promotion table

enter image description here


enter image description here

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download