Lead Software Engineer at Mumsnet .

Published on

Laravel DDD - Querying Records in our Domain

In the last instalment we spoke about using a simplified version of the CQRS pattern to create commands. This time we are going to talk about the other side to this and talk about queries.

A Query is a read action, a way to read data from external systems such as the database. As with before, we are going to carry on in our Blogging domain and this time we will try to read a Post entry based off a passed in slug.

We have already covered how to add these commands and queries into our container so we can bind and call them nicely. So I won’t go through that again, but instead we will expand upon it and the examples.

Our First Query

Our first query is to lookup a Post record based off of a passed in slug. To do this we don’t need as much boilerplate code as we did for the command - so this is a lot simpler. We will still want to create an interface/contract that we can bind to, but beyond that - there is no data objects or factories to create. First up we should always start with our interface/contract, as we need to think about the options and parameters we need to pass in.

5namespace Infrastructure\Blogging\Queries;
7interface FindPostBySlugContract
9 public function handle(string $slug): null|Model;

As you can see from the above code example, the requirements for this is a lot simpler than the command. All we need is the lookup string and it will return either null or an Eloquent Model.

5namespace Domains\Blogging\Queries;
7class FindPostBySlug implements FindPostBySlugContract
9 public function handle(string $slug): null|Model
10 {
11 return Post::query()->where(
12 column: 'slug',
13 value: $slug,
14 )->firstOrFail();
15 }

All we are doing here is querying using a where clause to lookup the record and return the first one - which will return either the instance or null. So our query is super simple.

We could write this query inside our application itself, however we with DDD we want to abstract this logic out of our application and into our domain so that if required it can be rebuilt. You could take these commands and queries a step further by casting these models to Data Objects - however that would take a lot of additional time to explain, so perhaps best left for another time.

Adding our Query to the container

Now we have our query we can add it to our container, so that as we said - we can inject the interface/contract and keep the implementation as interoperable as possible.

5namespace Domains\Blogging\Providers;
7use Domains\Blogging\Commands\CreatePost;
8use Domains\Blogging\Queries\FindPostBySlug;
9use Domains\Blogging\DataObjects\PostDataObject;
10use Domains\Blogging\Factories\PostDataObjectFactory;
11use Infrastructure\Blogging\Commands\CreatePostContract;
12use Infrastructure\Blogging\Queries\FindPostBySlugContract;
13use Infrastructure\Blogging\DataObjects\PostDataObjectContract;
14use Infrastructure\Blogging\Factories\PostDataObjectFactoryContract;
16class PostsServiceProvider extends ServiceProvider
18 /**
19 * @var array<class-string,class-string>
20 */
21 public array $bindings = [
22 PostDataObjectFactoryContract::class => PostDataObjectFactory::class,
23 PostDataObjectContract::class => PostDataObject::class,
24 FindPostBySlugContract::class => FindPostBySlug::class,
25 CreatePostContract::class => CreatePost::class,
26 ];

As before, we are simply adding the interface/contract and implementation into the bindings on the Posts Service Provider, which will be auto-registered through our Domain Service Provider.

Using our Query

To use the query in our controllers we can inject the interface/contract into the controllers constructor - and then call handle on it within our controller method itself. It is clean and relatively simple to replicate across your code base.

5namespace App\Http\Controller\API\V1\Posts;
7class FindController
9 public function __construct(
10 private readonly FindPostBySlugContract $query,
11 ) {}
13 public function __invoke(Request $request, string $slug)
14 {
15 $post = $this->query->handle(
16 slug: $slug,
17 );
19 return new JsonResponse(
20 data: new PostResource(
21 resource: $post,
22 ),
23 status: Http::OK,
24 );
25 }

In the above example we have an invokable controller that has one URL parameter slug which we can use to pass through to our Query and fetch the single record. We can then use this to return a Post Resource using the correct Post with a HTTP status OK (200).