April 202407:4319.5k views

Laravel + Cloudflare D1 speedrun

About

Using Cloudflare's D1 database with Laravel turns out to be incredibly easy... and opens the door to a lot of interesting use cases.

Transcript

Everything we said

00:00

Aaron Francis

Not that long ago, I made a video about my personal website, aaronfrancis.com. Great website, great video. Go watch the video. In that video, I talk about how I use SQLite as my database, and I commit it all to the Git repo and ship it off to Laravel Vapor. I thought this was really clever, so I told my friend and podcast co-host, Ian, about it, and he thought I was crazy.

00:23

So to get static site speeds, I have Cloudflare, which you love, active investor.

00:30

Unidentified

Love the Cloudflare. Oh man, it's up 5% today.

00:32

Aaron Francis

Love the Cloudflare.

00:33

Unidentified

Let's go!

00:34

Aaron Francis

Oh.

00:34

Unidentified

Let's go!

00:35

Aaron Francis

Check it every freaking day.

00:36

Unidentified

Cloudflare, baby.

00:39

Aaron Francis

So to get the static site speeds, I have this rule in Cloudflare that's just brute force cache-

00:46

Unidentified

I'm with you

00:46

Aaron Francis

... everything-

00:47

Unidentified

I'm with you all the way on this

00:47

Aaron Francis

... all around.

00:49

Unidentified

Cache everything.

00:49

Aaron Francis

And then, and then-

00:50

Unidentified

In-

00:50

Aaron Francis

And, and then-

00:51

Unidentified

Forever

00:51

Aaron Francis

... okay, so we're on, we're on the straight and narrow so far.

00:53

Unidentified

That's where-

00:53

Aaron Francis

SQLite, boring as could be. Laravel-

00:55

Unidentified

Perfect alignment

00:56

Aaron Francis

... Cloudflare, active investor.

00:58

Unidentified

I love it.

00:59

Aaron Francis

Okay, so not crazy yet.

01:01

Unidentified

Nope.

01:01

Aaron Francis

And so when I deploy, I make a API call to Cloudflare that says, "Blow everything up. I'm coming, baby."

01:07

Unidentified

Right.

01:07

Aaron Francis

And so it just, it just kills the cache.

01:09

Unidentified

Yep. I'm with you.

01:10

Aaron Francis

And then the next time somebody requests it, it'll hit Vapor, it'll run through Cloudflare, it'll cache.

01:14

Unidentified

Yep.

01:15

Aaron Francis

What is crazy about that?

01:17

Unidentified

So that's all not crazy. But I think-

01:19

Aaron Francis

That's all it is.

01:20

Unidentified

No, but you're leaving out a step. In the video, you had this whole other step where you do some local stuff, and then you push the local stuff to the server, but you don't need to do that stuff. It can-

01:29

Aaron Francis

What are you talking about? What are you talking about? Now, I'm never gonna tell Ian that he's right about anything, honestly, but especially not about this. One thing, one thing that is a little bit weird is committing that SQLite database to the Git repo. In that video, I talked a lot about Cloudflare and how I use their aggressive caching to make my site extremely fast, and they reached out and said, "Hey, we actually have a database that you can use.

01:56

Do you wanna try D1? It's generally available now. Do you wanna do a video on it?" And I said, "You bet I do." D1 is Cloudflare's serverless database offering. It's built on top of SQLite, and since it's serverless, you pay by usage instead of paying a fixed amount. I'll tell you more about that in a second, but before we do, I wanna do two things. The first is Laravel speed run.

02:17

How quickly can we get D1 working together with Laravel? And the second is what are some potentially interesting use cases once D1 is your database? Let's do the speed run first. You can see here that I have no D1 databases. You could create it right here by clicking Create Database. But we're gonna hop over to the terminal and say npx wrangler, and that's their little command line tool, d1 create, and we're just gonna call this, we'll call it blog-dev.

02:45

So we're gonna create a database. It should spit out a database ID, which you're welcome to try to use, but it's already been deleted. So I'm gonna copy this database ID and come put it in my Laravel environment, uh, file. There are a few other keys in here, like my, uh, API token and account ID. I am not gonna show you those because my account ID doesn't change.

03:10

So I'm just gonna put the database ID in here. And then to combine Laravel and D1, there's a package that sits in the middle called L1, which is a community package. Great name, D1 Laravel L1. Love it. I've already installed it 'cause that part's boring, so let's come over to the docs. L1 usage with raw PDO, interesting, not what I'm after.

03:30

Usage with Laravel, that is what I'm after. So I'm gonna copy out the D1 connection part, and we're gonna come over to database. And right here, we're gonna put D1. I don't need to change anything 'cause I already have these three environment variables set up. Um, it does go over, it does go over Cloudflare's API, which is why you need a custom driver for this.

03:51

So we're gonna come up here and change the default from SQLite to D1. Now, with any luck, we can hop over here, clear this out, php artisan migrate. We should be running these migrations against our D1 database. If we come back to the dashboard and refresh, you see we do have the blog-dev database, and we look, there are all the tables.

04:15

And if you wanted to poke around with the data, you could click into one. This is the Laravel data. I didn't populate my tables with any data, so that's the only table that has anything in it. Not bad for a speed run, right? That took, I don't know, maybe less than a minute, which is pretty good. I'm pretty happy about that. I'm just testing this from my local machine, so there is a little bit more latency from where I am to where the database is, and I think that's an interesting thing that we might need to consider is this database is typically supposed to be used

04:45

with Cloudflare workers or Cloudflare pages, but we're using it with a full-on Laravel application. And this is where I think it gets interesting from a Laravel application standpoint. Yes, we just set up the database to be our Laravel database. We have Eloquent. That all works fine and dandy. You can also access that data from Cloudflare workers, which means you can manipulate or do or query the data before it ever reaches your Laravel application.

05:11

So you could do redirects, you could do caching, you could do responses and requests, headers. You could do all kinds of stuff before it ever gets to your Laravel app with the same data that your Laravel app is relying on. That's what I wanna hear from you on. What can you possibly dream up of doing with the combination of Laravel and Cloudflare workers?

05:32

I'm gonna show you two more things before we go. One is the insights that come with this database, and one is time travel, which is like automated backup that you never have to turn on, and it's point-in-time recovery. So let me show you, let me show you the insights first. Their insights are great 'cause you can query all different types of things from your database, so we're just gonna say insights on the database that we just created, sort by time, show us 10.

05:57

So this is gonna show us the 10 slowest queries, and this is all available via API, so it returns this nice JSON response. So you can potentially imagine how you could build this into some sort of alerting, reporting, notification system so you can keep track of what your slowest queries are. They do have a little bit of insights in the actual, uh, browser here.

06:19

You can come over here and see the queries and the latency and the metrics, that sort of thing. Um, but I like, I like using the API 'cause you can get exactly what you're looking for out of it. Metrics are very cool, but nothing is cooler than time travel. So let's talk about time travel generally. It is possible. However, you can only move forward through time, which kind of defeats the whole point, right? With Cloudflare's time travel for your database, which is slightly different, you can just travel back in time to any point in time in the past 30 days and restore your database.

06:49

There's nothing to configure. There's nothing to enable. More importantly, there's nothing to forget to enable. So you can just pick any point in time, hit Restore, and you are good to go. Y'all, I am always going to love databases, maybe now more so than ever. All of these books back here, those are all database books. I just got a bunch of new ones. The SQLite revolution is upon us.

07:12

I am here for it. I am excited. What I wanna hear from you in the comments is how do we combine Laravel with Cloudflare workers using D1 in the middle? There's something interesting there, and I want you to tell me what it is. Cloudflare just wrapped up their developer week, so they just announced a ton of stuff, including, uh, D1 going GA, so I'll leave a link to that in the description.

07:36

Thank you, Cloudflare, for helping me make this video. Until the next time, see ya.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues