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

Eudemon's avatar

Adding new API endpoint

I have a custom table created and stored custom data,

I want to create API end point to retrieve data from that table,

all the guides I read tells me to create a resource and resource controller,

then add the route to api.php

with a toArray function in CustomResource returning 'id' 'fields', but how does laravel know i want to pull my custom table? I don't see how they are linked together

when I access the api url /api/custom/{id} it return class 'custom' does not exist

0 likes
13 replies
douglasakula's avatar

@eudemon - At a basic level - the route will call the controller which will fetch data from the model - and the model is a database object fetching data from the table.

If you could post the controller code and specifics of where you are stuck / trying to do - you would get more refined help

SteveCove's avatar

Please show the relevant code, its not possible to help based on your description.

It sounds like you have created a resource controller without a corresponding eloquent model.

Eudemon's avatar

@douglasakula @stevecove

verified that I have migration and seeding done for the table i need to pull, it seems like I am missing the model that controller talk to database

I have resource file with

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class MovieResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'type' => 'movie',
            'id' => $this->id,
            'title' => $this->title,
            'format' => $this->format
        ];
    }
}

controller file that's boiler plate from running make:controller --resource controller

namespace App\Http\Controllers;
public function show($id)
    {
        return new MovieResource($id);
    }

I already have a model class that makes the DB call for get, update, delete operation for regular routes that retrieve data, which looks like this

namespace App\Http\Models;
public function get($num = null){
        if ($num === null){
            $return = DB::table('movie')->get();
        } else {
            $return = DB::table('movie')->select()->limit($num)->get();
        }

        return $return;
    }

but I don't know how to connect this to my API route which is Route::get('movie/{id}', 'MovieController@show');

kobear's avatar

Are you including the reference to the MovieResource model in your controller?

use App\MovieResource;

Also, your namespace for the model class is wrong, I think. It should be

namespace App;
Eudemon's avatar

Model class namespace is correct, I have used it in my normal controllers

and yes I have use App\Http\Resources\MovieResource; in API controller

how does MovieResource know which table to pull? shouldn't I specific that? or DI my model?

<?php

namespace App\Http\Controllers;

use App\Http\Resources\MovieResource;
use Illuminate\Http\Request;

class MovieController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return new MovieResource($id);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

kobear's avatar

Model class namespace is correct

In your code Snippet you have

namespace App\Http\Models;

If that is the case, then your use statement should reflect that:

use App\Http\Models\MovieResource;

As for where you reference the table, that is in the model class. By default, Laravel tries to find the table by pluralizing the class name. In this case, the table name would be movieresources

But you can hard code what table it is by including the following after your class declaration

protected $table="<table_name>";

See https://laravel.com/docs/5.7/eloquent#eloquent-model-conventions for more info

SteveCove's avatar

@kobear eloquent resources are not models, they are a transformation layer that works with models.

https://laravel.com/docs/5.7/eloquent-resources

@eudemon a resource takes an eloquent model as a constructor parameter, and transforms it into JSON. It does not connect to the database.

If you are not using eloquent (your code snippet looks like you are using query builder), then you will need to format the JSON yourself.

1 like
SteveCove's avatar

The example uses an existing eloquent model (which is where the database work is done), that is passed to the resource via constructor. The resource itself has no knowledge of the database.

Do you have an eloquent model for your movie table?

SteveCove's avatar

Yes, if you need to use eloquent resources.

However you might be overthinking the problem.

It sounds like you already have working (non eloquent) model class to query the movie table, so you could just use that in controller and return a JSON response.

1 like
Eudemon's avatar

Yes I can use my existing model class but I am doing this not for any project, just to learn more about laravel so I am purposely do anything I haven't done before

1 like

Please or to participate in this conversation.