응답 내용은 psql로 이동한 후 주어진 __toString(), "boolean"을 구현하는 문자열 또는 개체여야 합니다.
MySQL에서 pSQL로 Laravel App을 옮기면 바로 실행됩니다.저는 계속 이런 오류가 생겼어요.
응답 내용은 __toString(), "boolean"을 구현하는 문자열 또는 개체여야 합니다.
프로모션을 반환해야 하는 API가 있습니다.
http://localhost:88888/api/promotion/1
public function id($id){
    $promotion = Promotion::find($id);
    dd($promotion); //I got something here
    return $promotion;
}
 
예전에는 승진을 반납했는데, 지금은 오류가 발생했습니다.
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
}
 
내용
__ 이에 대한 어떤 힌트나 제안도 큰 도움이 될 것입니다!
의 응답은 의 를 .Response물건.  순 물건을 그냥 돌려줄 수는 없습니다.
다음과 같이 변경합니다.
return Response::json($promotion);
 
또는 도우미 기능을 사용하여 내가 가장 좋아하는 것:
return response()->json($promotion);
 
응답을 반환하지 않으면 인코딩 문제가 발생할 수 있습니다.이 문서 참조:응답 내용은 __toString(), \"boolean\"을(를) 구현하는 문자열 또는 개체여야 합니다."
TL;DR
 response()->json($promotion)이 질문에서는 문제가 해결되지 않습니다.$promotion는 응답을 위해 자동으로 json_encode되는 Expoent 객체입니다.다 .imgPHP 스트림 리소스인 property는 인코딩할 수 없습니다.
세부 사항
컨트롤러에서 무엇을 돌려주든지 라라벨은 문자열로 변환을 시도할 것입니다.때때인때__toString()변환을 위해 magic 메서드가 호출됩니다.
만 할 는.return $promotion의 관제사 로,입니다에 __toString()표시할 문자열로 변환합니다.
Model,__toString()toJson()합니다. 합니다.json_encode. ,json_encode입니다.false합니다.
당신의.dd당신의 것을 보여줍니다.img은입니다.stream resource.json_encode를 부호화할 수 .resource의 원인일   를해야 합니다.img$hiddenjson_encode.
class Promotion extends Model
{
    protected $hidden = ['img'];
    // rest of class
}
데이터베이스에서 ajax 호출을 사용하여 데이터를 가져올 때 이 문제가 발생했습니다.컨트롤러가 배열을 반환하면 이를 부울로 변환합니다.문제는 ú(어법이 있는 u)처럼 '잘못된 캐릭터'가 있다는 것이었습니다.
 object.json_encode그리고 돌려줍니다.이렇게 하면 적절하고 유효한 개체가 반환됩니다.
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);
}
 
다른 사람에게 도움이 될지도 모른다고 생각합니다.
파일에서 직접 지적되지 않아 오류가 발생하였습니다.그러나 실제로는 컨트롤러 파일에서 트리거됩니다.컨트롤러 파일 내부에 정의된 메서드의 반환 값을 부울 값으로 설정할 때 발생합니다.부울 유형에는 설정할 수 없지만, 반면에 설정하거나 문자열 유형의 값을 지정해야 합니다.다음과 같이 나타낼 수 있습니다.
public function saveFormSummary(Request $request) {
      ... 
      $status = true;
      return $status;
}
 
지정된 오류를 처리하기 위해 문제를 해결할 수 있는 방법으로 위의 부울 타입의 반환 값이 주어집니다.반환 값의 유형을 문자열 유형으로 변경하기만 하면 됩니다.
다음과 같이:
public function saveFormSummary(Request $request) {
      ... 
      $status = "true";
      return $status;
}
사용가능json_decode(Your variable Name):
json_decode($result)
 
저는 모델로부터 가치를 얻고 있었습니다.열이 이와 같은 값을 가지는 경우.
{"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
언급URL : https://stackoverflow.com/questions/38770871/the-response-content-must-be-a-string-or-object-implementing-tostring-bool
'programing' 카테고리의 다른 글
| 포인터가 가리키는 문자열 크기 찾기 (0) | 2023.10.12 | 
|---|---|
| mysql의 하위 쿼리 구분 기호 (0) | 2023.10.12 | 
| mariadb 설치 문제 - ERROR 1524 (HY000):'caching_sha2_password' 플러그인이 로드되지 않았습니다. (0) | 2023.10.12 | 
| 워드프레스의 발췌문을 "더" 링크와 함께 사용하시겠습니까? (0) | 2023.10.12 | 
| 왜 괜찮은 sql 파서가 없습니까? (0) | 2023.10.12 | 
