The Response content must be a string or object implementing __toString(), “boolean” given. in laravel

你说的曾经没有我的故事 提交于 2020-01-15 05:48:08

问题


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


Here is my code:

 Route::get('/user/ip', function(Request $request) {
    $ip =$request->ip();
    //return $ip;
    return Curl::to('https://ipapi.co/'.$ip.'/json/')->get();
   });

回答1:


I was looking for a similar question. Your code should be as:

Route::get('/user/ip', function(Request $request) {
    $ip =$request->ip();
    //return $ip;
    return response()
        ->json(Curl::to('https://ipapi.co/'.$ip.'/json/')->get()
    );
});

Good luck




回答2:


There is no need to create the api that way.

On Laravel 5 you can create your route on routes->api then Laravel will know that is a api route.

Example:

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
Route::get('/engineers', 'EngineerController@index');

The Engineers Collection:

class EngineerCollection extends ResourceCollection {

    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'data' => $this->collection
        ];
    }
}

And the EnginersController index function:

 public function index()
    {
        return new EngineerCollection(Engineers::all());
    }

And then go to your app:

http://myapp.com/api/engineers



来源:https://stackoverflow.com/questions/44667658/the-response-content-must-be-a-string-or-object-implementing-tostring-bool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!