Courses

Laravel API Code Review and Refactor

Order Owner Refactor to User Controller

You're reading a FREE PREVIEW of a PREMIUM course.

Link to the repository

[Only for premium members]

One more Controller that looked "suspicious" to me was OrderOwnerController.

And yes, the naming is confusing: there was OwnerOrdersController (deleted by now) and OrderOwnerController. Let's see what's inside.


OrderOwnerController: Do We REALLY Need This?

Here's the code of that Controller.

app/Http/Controllers/Api/V1/OrderOwnerController.php

namespace App\Http\Controllers\Api\V1;
 
use App\Http\Filters\V1\OwnerFilter;
use App\Http\Resources\V1\UserCollection;
use App\Http\Resources\V1\UserResource;
use App\Models\User;
use Illuminate\Http\Response;
 
class OrderOwnerController extends ApiController
{
/**
* Display a listing of the resource.
*/
public function index(OwnerFilter $filter)
{
return response()->json(new UserCollection(
User::with('orders')->filter($filter)->paginate()
), Response::HTTP_OK);
}
 
/**
* Display the specified resource.
*/
public function show(User $owner)
{
if ($this->include('orders')) {
return response()->json(new UserResource($owner->load('orders')), Response::HTTP_OK);
}
 
return response()->json(new UserResource($owner), Response::HTTP_OK);
}
}

Ok, so it lists the Users with UserCollection or UserResource.

It doesn't do anything with orders specifically, except for including the relationship when needed.

So, my question was: why was it called Order Owner Controller?

I decided to go on a quest: force a breaking change in this API (author, sorry if you're reading this) and...

The full lesson is only for Premium Members.
Want to access all 15 lessons of this course? (56 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord