در حال بارگذاری ...

Project Setup With Breeze0:00

One way you can prevent spam is through the honeypot technique. Let me show you. I'll create a brand new app. You'll see I have Laravel 8.4. Next, I do need authentication, so I'm going to pull in Laravel Breeze, which is basically the new version of Laravel UI. Simple authentication scaffolding. Now, I should be able to run php artisan breeze:install. That will update my routes file. That will give me some basic scaffolding to work with,

Configure SQLite Database0:28

That will update my routes file. That will give me some basic scaffolding to work with, and then we install our dependencies. Okay, so let's go ahead and open this in phpStorm. Now, the first thing I want to do is open my .env file, and let's use a simple SQLite database, and then that will be stored within the database directory. So I will create that here and then migrate the initial migrations. Okay, next, I know we're going to work with posts, just as an easy example.

Create Post Model Migration0:55

Okay, next, I know we're going to work with Posts, just as an easy example. So very quickly, I'm going to set up a Post model with a migration file, just so we can knock this out really quick. Okay, so as simple as we can get, a Post consists of, excuse me, a title as well as the body, and that will be a text column. All right, let's migrate that table, and yeah, I think we're ready to begin. So let's have a look in the browser,

and yeah, I think we're ready to begin. So let's have a look in the browser, because Breeze install set up our auth routes and a dashboard page. So we can go ahead and register a demo account. Here we go. So let's register John Doe. And there we go. We're signed in and on our dashboard. Okay, so let's go ahead and register two routes. First, if you visit post/create,

Add Post Routes Auth1:45

Okay, so let's go ahead and register two routes. First, if you visit post/create, that will load a view for a simple form. And then I'll do another one here. If you make a POST request to create the post, then persist the post. However, in both of these cases, you have to be signed in. So let's go ahead and add the auth middleware. All right. However, I need an easy link to it.

All right. However, I need an easy link to it. So why don't we put it right up here. We can go to our navigation file. Here's a nav link for the dashboard. So let's add another one for create post. And route, let's give it a route, like post.create. Just make sure you go ahead and add that here. Name post.create. Think of a route as a name that represents a URI.

Name post.create. Think of a route as a name that represents a URI. So now if I come back and give this a refresh, sure enough, I can create the post. But of course, the view doesn't yet exist. So we're about done with our setup. Thank you for coming along. Post directory, create.blade.php. And why don't we reuse the dashboard here? Create post.

Build Create Post Form2:54

And why don't we reuse the Dashboard here? CreatePost. This will be max width of xl. And then we're going to have a form here. Okay. So I have a couple snippets that I often use for a simple input that has Tailwind styling. It's a label, some classes. An input, some classes. And then a check for an error,

An input, some classes. And then a check for an error, a validation error for that input. Pretty common stuff, so it's a good idea to put that in a snippet. So we'll have one for the title and then another one for the body. And then finally, a quick one for a submit button. And we'll call that publish. And yeah, I'm going pretty quick.

Handle Validation Persistence3:51

When you submit this form, it'll hit this endpoint where we would then read that request data, validate it, and then persist it. So that might take the shape of something like this. Post::create, and then let's just validate it in line here. And we'll say the title, as simple as we can get it, is required, and the body is required as well. So if validation passes,

and the body is required as well. So if validation passes, the validated attributes only will be returned, which we then pass to post create. And then we'll say published. All right. Let's have a look. Give it a refresh. My first post. My first body.

Fix Mass Assignment4:29

My first post. My first body. Kind of weird. You run it. Ah, yeah, and we do run into a mass assignment issue. Out of the box, PlayerFL protects you against mass assignment vulnerabilities. But a little tip. If you know what you're doing, you can disable it in two ways.

If you know what you're doing, you can disable it in two ways. First, you could set the guarded property to an empty array. You don't need to guard anything for me. I'm in charge of that. The only downside is you'd have to do that for every Model. So instead, you might go to your app's ServiceProvider, and then here, disable it. Model::unguard(). I kind of do this for all my projects.

Model, unguard. I kind of do this for all my projects. Again, as long as you know what you're doing, there's no problem whatsoever. But be thoughtful. Anyways, I give it a refresh, and there we go. I'm going to open that SQLite file. Here's our User, and then here is that first Post,

Here's our User, and then here is that first Post, which I will delete. So now if I switch back, all of our setup is done. We have authentication. We have a nice landing page to create a Post. We have some validation. We have persistence. In the next episode,

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟