Skip to main content

Order Owner Refactor to User Controller

Premium
5 min read

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 of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

No comments yet…