37

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

enter image description here

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

code-8
  • 54,650
  • 106
  • 352
  • 604

6 Answers6

35

Your response must return some sort of Response object. You can't just return an object.

So change it to something like:

return Response::json($promotion);

or my favorite using the helper function:

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

If returning a response doesn't work it may be some sort of encoding issue. See this article: The Response content must be a string or object implementing __toString(), \"boolean\" given."

Community
  • 1
  • 1
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123
  • Please check the post I referenced to see if you have some sort of utf8 encoding issue. Apparently this is somewhat common for some. An encoding issue with data could be causing this. Everything looks normal with your model but could be some underlying issue with the table itself. – Jared Eitnier Aug 04 '16 at 15:10
  • Is it something wrong with create_at / updated_at ? For some reasons, your suggestions still throwing the same **error** – code-8 Aug 04 '16 at 15:22
  • Maybe you can try adding `created_at` and `updated_at` params to `$hidden` array in the model? That was another suggestion that worked for some. If that works we can narrow it down from there. – Jared Eitnier Aug 04 '16 at 15:58
  • @JaredEitnier It doesn't have to be a `Response` object. You can return any string or any object that implements the `__toString()` method (which Eloquent Models do). – patricus Aug 04 '16 at 16:31
28

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
}
patricus
  • 59,488
  • 15
  • 143
  • 145
  • Hmm.. good call on the img though, Now, I got this. `Call to undefined method stdClass::toJson()` I hope I am close. – code-8 Aug 04 '16 at 16:36
  • @ihue I would suggest creating a new question with updated code and information for that new issue. – patricus Aug 04 '16 at 16:54
  • I ended up remove the `img` column from my promotion table, and it work back again. I guess that psql is really strict. – code-8 Aug 04 '16 at 17:29
  • I couldn't do it without your point me to the right direction though. Thanks. You safe my life.. :) – code-8 Aug 04 '16 at 17:29
  • very great check .. so i will use dd() instead of return in some cases – mercury Feb 07 '17 at 16:22
3

I got this issue when I used an ajax call to retrieve data from the database. When the controller returned the array it converted it to a boolean. The problem was that I had "invalid characters" like ú (u with accent).

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
3

So, rather return the whole object first, just wrap it to json_encode and then return it. This will return a proper and valid object.

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

Or, For DB this will be just like,

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

I think it may help someone else.

Maniruzzaman Akash
  • 4,610
  • 1
  • 37
  • 34
0

It is being pointed out not directly in the file which is caused the error. But it is actually triggered in a controller file. It happens when a return value from a method defined inside in a controller file is set on a boolean value. It must not be set on a boolean type but on the other hand, it must be set or given a value of a string type. It can be shown as follows :

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

as follows :

public function saveFormSummary(Request $request) {
      ... 
      $status = "true";
      return $status;
}
Hemant Kumar
  • 1,025
  • 15
  • 32
0

You can use json_decode(Your variable Name):

json_decode($result)

I was getting value from Model.where a column has value like this way

{"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"}]}
           ]
}

so access this value form model. you have to use this code.

$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'] ;

You can also define caste in your Model file.

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

so after this no need of this line .

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

you can directly access this code.

$settingAttendance->bio_attendance_day_time
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pankaj
  • 1
  • 17
  • 36