Search code examples
phplaravellumen

API call in postman return message undefined index: data


{
    "error": {
        "title": "Internal Server Error",
        "message": "Undefined index: data, in file D:\\Projects\\mika-api\\vendor\\illuminate\\support\\Collection.php at Line 1290"
    }
}

i got that error message in postman when calling endpoint : localhost/mika-api/public/v1/transaksi-pindah-jurusan/

hope someone can tell me whats wrong with this, i'm already check it again and again but find nothing to fix it

This is my controller :

<?php

namespace App\Http\Controllers;

use App\UseCases\CollectionRequestModel;
use App\UseCases\TransaksiPindahJurusan\GetTransaksiPindahJurusanInteractor;
use Illuminate\Http\Request;

class TransaksiPindahJurusanController extends Controller
{
    private $getTransaksiPindahJurusan;

    public function __construct(GetTransaksiPindahJurusanInteractor $getTransaksiPindahJurusan)
    {
        $this->getTransaksiPindahJurusan = $getTransaksiPindahJurusan;
    }

    public function getCollection(Request $request)
    {
        $result = $this->getTransaksiPindahJurusan->getCollection(new CollectionRequestModel($request->all()));

        $total = $result['total'];
        unset($result['total']);

        $pagination = $this->getPagination($request, $total, $request->except(['limit', 'offset']));

        return $this->jsonResponse200Collection($result['data'], $total, $pagination);
    }
}

This is my interactor

<?php

namespace App\UseCases\TransaksiPindahJurusan;

use App\Repositories\PMBP\TransaksiPindahJurusanRepository;
use App\UseCases\CollectionRequestModel;

class GetTransaksiPindahJurusanInteractor implements GetTransaksiPindahJurusanInterface
{
    private $repoTransaksiPindahJurusan;

    public function __construct(TransaksiPindahJurusanRepository $repoTransaksiPindahJurusan)
    {
        $this->repoTransaksiPindahJurusan = $repoTransaksiPindahJurusan;
    }

    public function getCollection(CollectionRequestModel $request)
    {
        $result = $this->repoTransaksiPindahJurusan->findManyBy(
            ['*'],
            $request->getParameters(),
            $request->getOrderBy(),
            $request->getGroupBy(),
            $request->getLimit(),
            $request->getOffset()
        );

        return $result;
    }
}

This is my interface

<?php

namespace App\UseCases\TransaksiPindahJurusan;

use App\UseCases\CollectionRequestModel;

    interface GetTransaksiPindahJurusanInterface
    {
        public function getCollection(CollectionRequestModel $request);
    }

this is my repository

<?php

namespace App\Repositories\PMBP;

use App\Repositories\BaseRepository;
use Illuminate\Support\Facades\DB;

class TransaksiPindahJurusanRepository extends BaseRepository
{
    protected $tableName = 'pmbp.transaksi_pindah_jurusan';

    public function findManyBy($selects = [], $parameters = [], $orders = [], $groups = [], $limit = null, $offset = null)
    {
        $query = $this->find($selects, $parameters, $orders, $groups);
        $totalData = $query->count();
        $this->addlimit($query, $limit, $offset);

        $result = $query->get();
        $result['total'] = $totalData;
        return $result;
    }

    public function find($selects = [], $parameters = [], $orders = [], $groups = [])
    {
        $query = DB::table($this->tableName)->select($selects);
        $this->addParameters($query, $this->tableName, $parameters);
        $this->addOrders($query, $this->tableName, $orders);
        $this->addGroups($query, $this->tableName, $groups);

        return $query;
    }

    public function findOneBy($selects = [], $parameters = [])
    {
        return $this->find($selects, $parameters)->first();
    }
}

Solution

  • Looks like $result['data'] is undefined. Before return do dd($result); and see if there is 'data' present.