The content of the response must be a string or an object that implements __toString (), "boolean", specified after switching to psql

As soon as I transfer my Laravel application from MySQL to pSQL. I kept getting this error.

The content of the response must be a string or an object that implements __toString (), "boolean".

I have an API that would suggest returning my ad

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

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

He used to return my advertisement, now it returns 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

enter image description here

__ Any tips / suggestions on this would be a huge help!

+16
php mysql psql laravel laravel-5
source share
4 answers

Whatever you return from your controller, Laravel will try to convert to a string. When you return the object, the magic method of the __toString() object will be called to perform the conversion.

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

In Model __toString() calls toJson() , which returns the result of json_encode . Therefore, json_encode returns false , which means that it is encountering an error.

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

 class Promotion extends Model { protected $hidden = ['img']; // rest of class } 
+13
source share

Your answer should return some kind of Response object. You cannot just return an object.

So change it to something like:

 return Response::json($promotion); 

or my favorite, using a helper function:

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

If returning the response does not work, it may be some kind of encoding problem. See This article: The content of the response must be a string or an object that implements __toString (), \ "boolean \". Given "

+21
source share

I had this problem when I used an ajax call to retrieve data from a database. When the controller returned the array, it converted it to a logical one. The problem was that I had "invalid characters" such as ú (u with an accent).

+3
source share

This is not indicated directly in the file that caused the error. But it actually runs in the controller file. This happens when the return value from the method defined internally in the controller file is set to a boolean value. It should not be set for a boolean type, but, on the other hand, it must be set or have a string type value. This can be shown as follows:

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

Given the return value of the logical type above in the method, in order to be able to solve the problem for handling the specified error. Just change the return type to string type

following:

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

It depends on the scenario. The above condition is met when the page is sent to a method called saveFormSummary simply to verify that the content inserted in the form is correct. If the validation process is successful, it will return the boolean value back to the blade presentation file template and move on to another process again.

0
source share

All Articles