Introducing Laravel Data0:00
There is one Sparsity package that we need to talk about, which is kind of special because it's not that small. I'm talking about Laravel Data, which is actually a pretty big package. But there is one specific use case which I want to show you, which is quite simple to do, and that's why it still fits very well on this list here. So here we are on the documentation page of Sparsity's Laravel Data package. And if you have no idea about what data objects are, here's also a good video linked here by Freik, who is explaining what data objects are, and he's also explaining Laravel Data in detail. But for our use case, there's a very small use case.
Reviewing Post Store Test0:34
who is explaining what data objects are, and he's also explaining Laravel data in detail. But for our use case, there's a very small use case that I really like to use, even in small projects. Mostly people would discuss that data objects are more used in big applications, but yeah, there's one use case I really like and one specific thing that I want to do in a lot of my applications. So let's go back to the application and specifically to this test here, where I want to make sure that a Post could be stored. All right, what does this test do? We're sending a POST request to a specific route.
All right, what does this test do? We're sending a POST request to a specific route. We're providing some details like a title, a content, and a category. So here we have an enum with some specific statuses for our category. And then we want to make sure that the request was successful and we want to make sure that we see in our database only one item and this should be exactly with these details. All right, let's give it a try. And as you can see, this is working. And let's also check out a little bit the code.
Current FormRequest Approach1:32
And as you can see, this is working. And let's also check out a little bit the code. So this is our route. We're sending a POST request to this controller here. Here we have a form request. Let's take a look at this, where we have defined some rules for our details. The title should be required, a string, the content should be required, and the string and the category should be one of those enums. And by default, authorize is set to true here. All right, and then inside our controller,
And by default, authorize is set to true here. All right, and then inside our controller, we are just getting the validated data from the form request and we are creating a new post. And we have seen that it works. So what I don't like about this approach here, so there's nothing wrong with this approach, but I'm not a big fan of using form requests, especially because this is something that could also be done with DataObject. So since it's a good idea to use DataObject,
Installing and Creating PostData2:18
especially because this is something that could also be done with DataObject. So since it's a good idea to use DataObject, then it's also a good idea to use them instead of form requests. So let me show you how this works. But first, we need to install a package from spaCy. All right, let's copy the command. Let's bring in this package. Here we go. And then let's create now a new DataObject for our Post. So under app, I already got a support folder.
And then let's create now a new DataObject for our Post. So under app, I already got a support folder. And here I'm going to provide a new directory called data for our DataObject. And here we're just creating a new php class, which we're calling just PostData. And we want to extend, which we can also define here already, the DataObject by spaCy level data. Here we go. Let's do this. So the only thing that we have to do here now is to provide a constructor.
Let's do this. So the only thing that we have to do here now is to provide a constructor. And here we're going to define our properties inside the constructor. So what do we need? We need a title, which should be a string. What else do we need? We need content, which will also be a string. And then last, we also need a postCategory, which should be of the type PostCategory. And let's just call it category.
which should be of the type PostCategory. And let's just call it category. And as you've seen, most of the things that we would do with FormRequest, we already do here. We are saying that this should be a string. And since it's not nullable, this is required. This is a string, and it's also not nullable. We could make it nullable, but we don't do this because we want to make this. And here we're going to say category should be of the type PostCategory. So I hope you already see the similarities.
And here we're going to say category should be of the type PostCategory. So I hope you already see the similarities to what we do inside our rules method of the FormRequest, because this is pretty much the same. All right, so the only next thing that we have to do now is inside our controller, we are getting rid of the FormRequest, and we are type hinting postData. And let's also call this postData. And then we don't have the request anymore, but we have our postData, which we can transform to an array.
Validating with Data Objects4:30
And then we don't have the request anymore, but we have our post data, which we can transform to an array. And now let's go back to our test. Let's try this out. And as you can see, this is still working. Let's just try to change something here, like this, for example. Let's make this a string, which shouldn't be allowed. Let's run the test again, and now it's fading. The selected category is invalid because it's not of the type post category. And how cool is this again?
The selected category is invalid because it's not of the type post category. And how cool is this again? So inside our controller, we have just type hinted post data. Let's maybe dump it out as well so that you can see it. Here we get then a post data object with our details being set up here already. And this is an approach that I really enjoyed lately, because here we can get rid of our form request. Let's kill this, get away, because here we have already everything defined as we would inside our post data. So you might say, what's now the difference?
as we would inside our Post data. So you might say, what's now the difference? So, okay, we're using a data object, but before we used a FormRequest. So it feels like we're just switching out one class for another. And that's true. First, I find that defining here the rules just through the constructor, just by defining the Post data object itself is very appealing to me. So I really like this compared to using the rules array. And secondly, now we've got a Post data object, which we can use inside our application for many other things.
And secondly, now we've got a Post data object, which we can use inside our application for many other things. And the bigger your application grows, the more you have to provide specific data and send this through the application or maybe to the front end to an Inertia React application. And then it's very useful to have those Post data because then you don't have to provide arrays, which I really think is not that useful because arrays always could be anything. But here we are strongly typed. So this means we only get the data that we're supposed to get.
But here we are strongly typed. So this means we only get the data that we're supposed to get. But of course, this is also just the beginning of what this package can do for you. So what you also could do is now say that inside this post data, you're going to nest other data object as well. And the form request will still work because it still would check first everything that's here. But then if you provide here another data object, it will also validate this one.
But then if you provide here another data object, it will also validate this one. So this means you can nest data object, which is super powerful and way easier for validating than just using form requests. And also, of course, if we go to the documentation, you can see that this is also quite big. So there are a lot of things that you can do. So you can also, let's go for it together. Nesting is what I already mentioned.
Exploring Advanced Features7:04
So you can also, let's go for it together. Nesting is what I already mentioned. You can use collection. So what we can do here is seeing that we're not expecting just one song data object, but a collection of those. And this is how we can define this special syntax from this package, which you can use. And then, yeah, as I mentioned, validation will also work for this. And then you can also use this data object as resources, which you want to provide from your API.
And then you can also use this data object as resources, which you want to provide from your API. And you can see that this will automatically be created as JSON as well. If you're going to return this, and also very interesting, you can transform data objects to TypeScript. So inside this package, there's also a TypeScript transformer, which you can run. And what this does is it takes your data object, like defined here, and it will create TypeScript types. So especially if you're working with React or a very typey application,
like defined here, and it will create TypeScript types. So especially if you're working with React or very typey application, this is super useful because then you have auto-completion in your IDE and type safety from your backend to your frontend. And the only thing that you have to do is run this specific command for re-application. So you don't have to write all those type definitions yourself because, yeah, you have already prepared this on your backend side and you don't want to write those again on the frontend side. So this is also something that's super powerful.
and you don't want to write those again on the frontend side. So this is also something that's super powerful and which I use a lot in a very big application. And yeah, there's a bunch more what this package can do. Check it out for yourself. It's super powerful. Or again, take a look at the talk by Freik about it. I think we have this here. Yeah, so at Laracon US, Freik gave an introduction to Level Data. So give this a look as well.
Yeah, so at Laracon US, Freik gave an introduction to level data. So give this a look as well. I'm a big fan of this package. Let's break down what we just learned. level data is a great but huge package. But using it for validating our data is just amazing. And that's why we have it here on this list. Thank you. Spasi.
Spasi.
