Creating a Controller and Model in Laravel

I recently started building a new SaaS product for note-taking. It’s very early on in development. This series of posts serves partly to remind me how to do something, and also as a way to help anyone who is searching. This series is not intended to be a comprehensive series of posts to cover everything Laravel, but rather just what I come across and need reminding of at a later date.

The first part of my product, called Notic, is to design and build the API. Right now I am simply creating a single migration to create a table to store notes. To make this work I need a model and a controller. Laravel provides a way to create a model and a controller either with separate commands or with a single command.

To create a model you can use the following:

php artisan make:model Notic

This creates an almost empty model class called Notic. Notice that I use the singular name here with an uppercase first letter (or CamelCase if that was needed).

I can then create a controller with the following:

php artisan make:controller NoticsController

This creates an empty controller called Notics. You can add options to the end of the controller. One such option is -r (or –resource) which creates 7 standard actions that are common across resource controllers, those being index, create, store, show, edit, update, and destroy. Because I am building an API, I can opt to use –api which adds the same except for create and edit which are typically not required. The command for this is as follows:

php artisan make:controller API\\NoticsController --api

Notice that I added API\\ just before the controller name. This creates a folder called API to put the Notics controller in. The –api generates the remaining 5 resources.

Rather than running two commands separately for creating a model and a controller, we can group them together as follows:

php artisan make:controller API\NoticsController --api --model="Notic"

The –model=”Notic” is an alternative to just using -m, but the benefit is that it keeps the naming convention with the controller being plural and the model singular.

If you look in the NoticsController file you will see code that looks like this:

/**
 * Display the specified resource.
 *
 * @param  \App\Models\Notic  $notic
 * @return \Illuminate\Http\Response
 */
public function show(Notic $notic)
{
    return $notic;
}

Notice that the function show now has Notic $notic that can be passed in through route model binding. If you create the controller separately as a resource or API then you receive a generic Request in there instead.

The route for this might look like this:

Route::get('/note/{notic}', [\App\Http\Controllers\API\NoticsController::class, 'show']);

If you run php artisan serve you then have the ability to navigate to a URL such as http://127.0.0.1:8000/note/1 and if a note with an ID of 1 is in the database, it will be returned.

Of course, returning $notic isn’t where this will end up, but this shows how a simple single command can be used to generate the controller and model and how you can add in a route and fetch an item from the database by ID.

Leave a comment