问题
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