Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins
Tags: laravel, remotejoins
Published: 2021-01-05 05:28:06

---
title: Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins
published: true
date: 2020-12-30 06:54:08 UTC
tags: Laravel, RemoteJoins
canonical_url: https://hasura.io/blog/building-graphql-api-laravel-postgresql-hasura-remote-joins/
---


 ![Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins](https://hasura.io/blog/content/images/2020/12/LaravelAPI_Postgres_RemoteJoins-7.png)

_This articles was written by [Chimezie Enyinnaya](https://twitter.com/ammezie) as part of the **Hasura Technical Writer Program**. If you would like to publish an article about Hasura or GraphQL on our blog, apply [here](https://bit.ly/3hltrtT)._

_Remote Joins_ in Hasura extend the concept of joining data  across tables, to being able to join data across tables and remote data  sources. Once you create relationships between types from your database  and types created from APIs, you can then “join” them by running GraphQL  queries.

Remote joins can join across the database and APIs.  These APIs can be custom GraphQL servers you write, 3rd party SaaS APIs,  or even other Hasura instances.

In this article, we will be building APIs in Laravel using PostgreSQL as a database and Hasura for query and Remote Joining multiple tables in the database.

## **Prerequisites**

- Knowledge of Laravel
- Basic knowledge of PostgreSQL and GraphQL

## **Setting up**

To start, we will need to install Laravel on our computer and set it up. Open your command line and type in the below command:

```
bash
composer create-project laravel/laravel hasura-graphql
cd hasura-graphql
php artisan serve
```

Next, we set up an online PostgreSQL database. I will be using [ElephantSQL](https://www.elephantsql.com/)for my database. You can use other online PostgreSQL databases if you would like.

After setting up our ElephantSQL account and creating your database, we will then set up a [Hasura.io](https://hasura.io/) account and create a new project. If you are new to Hasura, you can learn how to set up a new project [here](https://hasura.io/docs/1.0/graphql/core/getting-started/index.html#getting-started).

![Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins](https://hasura.io/blog/content/images/2020/12/Laravel-1-2.png)

Then we proceed to database setup, copy your PostgreSQL database URL and paste in the **Enter your database URL** input.

![Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins](https://hasura.io/blog/content/images/2020/12/Laravel-2-2.png)

_ **Note:** the URL starts with **postgres://** so look out for a URL like that in your online PostgreSQL platform._

Go to your database and create a **_todos_** table and populate it with some data.

![Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins](https://hasura.io/blog/content/images/2020/12/Laravel-3.png)

With that done, we are ready to test our database. We go back to our project and configure our .env file to add our database configuration.

```
DB_CONNECTION=pgsql
DB_HOST=<DATABASE_HOST>
DB_PORT=5432
DB_DATABASE=DATABASE_NAME>
DB_USERNAME=<DATABASE_USERNAME>
DB_PASSWORD=<DATABASE_PASSWORD>

```

After the configuration is complete, we need to install [lighthouse php](https://lighthouse-php.com/4.18/getting-started/installation.html#install-via-composer) to use GraphQL in our Laravel application. Lighthouse is a framework for serving QueryQL on Laravel. To install use the below code:

```
bash
composer require nuwave/lighthouse

```

Then we publish the Schema using:

```
bash
php artisan vendor:publish --tag=lighthouse-schema

```

Next, let’s install GraphQL Playground to test out our GraphQL API. To do that, simply use:

```
bash
composer require mll-lab/laravel-graphql-playground

```

Then we can open it using `http://127.0.0.1:8000/graphql-playground`. But before that, we need to configure our Lighthouse and create a Todo Model. We do that using the below command:

```
bash
php artisan make:model Todo

```

This will create a new model in your `app/Models` directory. Configure it with the code:

```
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Todo extends Model
{
    use HasFactory;

    protected $table = 'todos';
    protected $primaryKey = 'id';

    protected $fillable = ['title', 'short_description', 'content'];
}
```

Then, in our `graphql/schema.graphql` file we can add the schema for the `Todo` type as well as a couple of GraphQL queries:

```
type Query {
    todos: [Todo!]! @all
    todo(id: ID @eq): Todo @find
}

type Todo {
    id: ID!
    title: String!
    short_description: String!
    content: String!
    created_at: DateTime!
    updated_at: DateTime!
}
```

With that done, we can open our Graphql-playground and test out our database using `http://127.0.0.1:8000/graphql-playground`

![Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins](https://hasura.io/blog/content/images/2020/12/Laravel-4.png)

## **Remote Joins**

Now we are done with setting up, we need to create our remote joins, to do this we will be

- Modifying our `todo` table and added `users` table
- creating a relationship between the `users` table and `todo` table (manually populating data)
- Testing out our remote joins

To modify the database, go to your Hasura console and create a new `Users` table

![Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins](https://hasura.io/blog/content/images/2020/12/Laravel-5.png)

Then create a relationship between `todos` table and `users` table. To do that, follow the instruction [here](https://hasura.io/docs/1.0/graphql/core/schema/remote-relationships/action-relationships.html#action-relationships). Once the relationship is created between todos and users, we can then test on our Hasura console.

![Building GraphQL APIs with Laravel, PostgreSQL and Hasura Remote Joins](https://hasura.io/blog/content/images/2020/12/Laravel-6.png)

As the above code shows, we have joined our todos table to our `users` table and we are fetching all todos with their corresponding user.

## **Conclusion**

In this tutorial, we have

- Created a new instance of Laravel
- Installed and used Lighthouse and GraphQL playground for our graphQL query
- We then created our ElephantSQl database and tied it to our Hasura account.
- Configured our Laravel application to interact with our ElephantSQL and then created a remote join for our `todos` and `users` table. 

You should now understand the basics of building Laravel APIs with PostgreSQL and Hasura Remote Joins.

_This articles was written by [Chimezie Enyinnaya](https://twitter.com/ammezie) as part of the **Hasura Technical Writer Program**. If you would like to publish an article about Hasura or GraphQL on our blog, apply [here](https://bit.ly/3hltrtT)._

* * *

## About the Author

Chimezie Enyinnaya is a software developer and content creator based in Lagos, Nigeria. He is the founder of [adonismastery.com](http://adonismastery.com) where he teaches about [AdonisJS](https://preview.adonisjs.com), a fully-featured Node.js framework.