This episode is for members only

Sign up to access "Build A Static File Blog with Laravel" right now.

Get started
Already a member? Sign in to continue
03. Working with Sheets


By the end of this episode, we will have created a couple of blog posts.
We will be able to dump them out on the index just here, ready to start iterating through and extracting more data from. But the question is, how do we grab these files and actually get this information in the first place?
Well, there's a really, really good package called Sheet, which will allow us to store and retrieve static content in plain text files. That includes Markdown files, and it does actually include a parser for Markdown as well, which we are going to be tweaking a
little bit later to change around our syntax highlighting. But we can go ahead and just install this package, create a couple of files, set up a disk and just see these files rolling. So if we head over to the installation
section for this, let's go ahead and just pull this in as per the instructions. We'll get the config published as well, because we are going to need to modify that as well. And then we can just start to create some files out and dump these out on the page.
So we're actually going to start over in the config for Sheets here, because this is really important. The first thing that we need to do is set up a collection so we can have multiple collections of things depending on the content that we're pulling in.
In our case, we just have one because we just have posts within our blog. But if you later on had other things that you needed to pull in, you could go ahead and configure them as well. So let's get rid of the comments wrapping this and let's take a look
at the things that we need to configure here. The first thing for our posts collection, which we're not going to rename, although you can rename these if you want to, is the disk.
So this will be the Laravel file system disk that these specific files within this collection are read from. So we're going to need to configure that in just a minute when we choose where we want these to come from, and these can come from anywhere.
We're going to be putting them on our file system within our application. But these could come from a remote file system somewhere if you wanted to. Now, the second thing here is the sheet class. So every single file that we read in with this package will be bound to a class so
we can kind of work with it like an Eloquent model. We're obviously not going to be using Eloquent because we're not hitting the database, but we can still create a model to represent each of our objects or posts, which means we can add helper methods onto these and all that kind of stuff.
The third thing here is the path parser, which will be how these files are read. So we may need to change that later, but we'll see how we go when we start to create these files out and the structure we want these two files to be in. Then we've got a content parser, which we are definitely going to change.
And we're even going to go ahead and create our own parser a little bit later when we get to syntax highlighting. And then last of all, obviously, self-explanatory will be the extension of these files.
So already I know that we're going to be working with Mark down here. So I'm going to change this over immediately to MD. OK, so let's start with the sheet class because we don't have a class for this at the moment.
I'm just going to go ahead and create out a model with Laravel. So I'm going to call this post, of course, not creating a migration or anything like that because we're not dealing with our database. Now, if we head over to that post model,
we don't want this to extend an eloquent model because we're not hitting the database. What we want this to extend is sheet from that package that we've just created, and let's just re-index things here to pull that in. So we want this to extend the base sheet class just here from this package so we
can get rid of model now and we can even get rid of has factory because we don't necessarily need this here. So that's pretty much our post model set up ready to go. When we start to read in all of the posts
that we have within this specific location, they will now be represented as post models. So the last thing to do is to go ahead and figure out where we want these to be pulled from. Now, to do this, we're going to head over to our file systems config within Laravel,
which contains multiple disks. We've got local, public, S3. So as I said before, you could store these on S3 if you wanted to. But what we're going to do is just keep this really simple and we're going to have
a local driver, but we're just going to call this posts. And of course, you can tweak this around later depending on where you want to store these. If you want to store them remotely, it's entirely up to you. So I'm going to call this disk posts.
So that's the disk that we're referencing just here. Now, the driver is going to be local and we're going to put these in our storage path under app, but we're going to put these under a hosts directory. So let's create that directory out.
So let's find our resources section here. One of our resources, our storage section under app. We're going to create out a host directory. The reason I'm putting in here is this is not public.
So no one will be able to access the original Markdown files unless they were a public file. So they are private. No one can access them. But of course, this package can still read them.
OK, now we've done this. What we can have a go at is just dumping out what we've got so far. And of course, we're not going to get any files in here. So let's come over to our post index
controller and just play around with how we access these. So to access these, rather than using the model directly, we use these sheets facade and then we go ahead and specify the collection that we want to use. We know that's posts.
We've already defined that. That is what we want. Let's go and just say all. So there's a couple of methods that we can use here.
We can use get to get an individual one. But we're just going to dump out all of these. So that's die dump on every single one of these. And we will head over to the browser and just give this a refresh.
OK, so what have we got here? We've got a standard Laravel collection. Nothing is different to if we were working with the database. And once we start to create our files,
we're going to end up with a collection of posts. So let's start to create our file and just see what we get. So we're going to go over to our posts directory here, and of course, this is where we need to be careful
about the structure of how we create this out. So the default structure of this will be the date, first of all. So we're going to go ahead and use the year month day format to be really, really careful of that.
So let's just say what day are we on today? Let's just say the 28th of this month. And then we're going to go ahead and use a dot. And then we're just going to give the slug of the post.
So let's call this building a blog in Laravel. And of course, we're going to end that with a markdown extension. So now that we've done that, let's head over and just see the difference here. So, OK, at post not pound,
that's probably a good thing that we've caught that. Now, if we come over to config and sheet, let's make sure we use app models and post in here, give that a refresh. And here we go.
We've now got one item in this collection, which is a post. We have got the path to this and we have several attributes here as well. So this package has converted the date into a carbon instance for us, which is really handy because now we can
format this date in any way that we want. We have got a slug, which is great. We can use that for a root model binding a little bit later, which we're going to need to create our own custom root model binding for.
And we've got the contents of this thing. So at the moment, of course, that is completely empty. Now, what we want to do with this is go ahead and add in a little bit more information for this specific blog post.
So remember from the introduction, we saw that we had three hyphens here and then we had a bunch of properties like the title of the article. And then down here we had the body of the article. We're going to go ahead and change that up in a bit.
But let's head over to the next episode and start to list these blog posts out over on the index. First of all, style it up and then we can go ahead and check in how we can introduce this kind of metadata.
13 episodes1 hr 11 mins


Let's build a ridiculously fast blog from scratch in Laravel, entirely driven by markdown files. No database required!

Your blog will feature everything you're used to, like syntax highlighting, post tagging and pagination. Plus, it'll render in record time.

Once you're done with the course, you'll be able to add on additional features with ease, style it up how you want and quickly create new posts since there's no need for an admin panel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.