Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codebullet's avatar

Error Getting data through API Laravel 5.8

Good day all, Please I need some help, for some reason, I can't get data through API I get this error

            $this->data = $data->toJson($this->encodingOptions);
        } elseif ($data instanceof JsonSerializable) {
            $this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);
        } elseif ($data instanceof Arrayable) {
            $this->data = json_encode($data->toArray(), $this->encodingOptions);
        } else {
            $this->data = json_encode($data, $this->encodingOptions);
        }
 
        if (! $this->hasValidJson(json_last_error())) {
            throw new InvalidArgumentException(json_last_error_msg());
        }
 
        return $this->update();
    }
 
    /**
     * Determine if an error occurred during JSON encoding.
     *
     * @param  int  $jsonError
     * @return bool
     */
    protected function hasValidJson($jsonError)
    {
        if ($jsonError === JSON_ERROR_NONE) {
            return true;
        }
 
        return $this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR) &&
                    in_array($jsonError, [
                        JSON_ERROR_RECURSION,
Arguments
"Malformed UTF-8 characters, possibly incorrectly encoded"

Here is my API route


<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

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

Route::get('projects', 'API\ProjectsController@index');

and this is my Api/ProjectsController


<?php

namespace App\Http\Controllers\Api;

use App\Project;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProjectsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return Project::latest()->paginate(10);
    }

I tried resources I get the root URL HTML content

I appreciate your help

0 likes
3 replies
codebullet's avatar

Even i tried to import Agolia it gives me this error

could it be issue with packages?


C:\laragon\www\testate>php artisan scout:import "App\Project"

   InvalidArgumentException  : json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded

  at C:\laragon\www\testate\vendor\algolia\algoliasearch-client-php\src\RetryStrategy\ApiWrapper.php:233
    229|                 $body = '';
    230|             } else {
    231|                 $body = \json_encode($body);
    232|                 if (JSON_ERROR_NONE !== json_last_error()) {
  > 233|                     throw new \InvalidArgumentException(
    234|                         'json_encode error: '.json_last_error_msg());
    235|                 }
    236|             }
    237|         }

  Exception trace:

  1   Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper::createRequest("POST", Object(Algolia\AlgoliaSearch\Http\Psr7\Uri), ["KVVHQ6QFVS", "bfdb2210fe91a855c91f61044b2ef8a8", "Algolia for PHP (2.2.0); PHP (7.2.11)
; Laravel Scout (7.0.0)", "application/json"])
      C:\laragon\www\testate\vendor\algolia\algoliasearch-client-php\src\RetryStrategy\ApiWrapper.php:139

  2   Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper::request("POST", "/1/indexes/projects_index/batch", Object(Algolia\AlgoliaSearch\RequestOptions\RequestOptions), [])
      C:\laragon\www\testate\vendor\algolia\algoliasearch-client-php\src\RetryStrategy\ApiWrapper.php:89

  Please use the argument -v to see more details.


codebullet's avatar

Finally Got the issue,

It was a database table with invalid Json content, (nested Array)

All Solved now

Thank you @michalurva

Please or to participate in this conversation.