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

Installing GitHub Copilot0:00

All right, next up, check this out. This is really cool. So as you know, AI is all the rage right now, and there's a really popular tool from GitHub called Copilot. It uses OpenAI to suggest code and entire functions in real time right from your editor. And as it turns out, we can also use it within JetBrains projects. All right, let's give it a shot. So back to PHPStorm, command comma, let's go into Plugins, into the Marketplace, and I will search for Copilot. All right, this is the one. All right, it looks like we need to restart. All right, I think that should do the trick. Now, on your machine, you might get a prompt to first log in and authenticate with GitHub.

Generating Code Suggestions0:38

All right, I think that should do the trick. Now, on your machine, you might get a prompt to first log in and authenticate with GitHub. If so, follow those steps, and then you'll be all set to go. Now you have intelligent auto-completion courtesy of the OpenAI codecs. All right, so yeah, and immediately you get an idea of what you might want. But alternatively, you can do things like this. You write a comment that declares what you want to do. So a User has many conversations. And if we do this, sure enough, we are getting auto-completion or suggestion, and this is coming, again, from OpenAI, and I can tab complete.

And if we do this, sure enough, we are getting auto-completion or suggestion, and this is coming, again, from OpenAI, and I can tab complete. Now, in this case, just keep in mind, because we're using the Laravel Idea Plugin, we could also just start typing hasMany, and we can use the shortcuts here. hasMany activities or posts or comments, and that will populate it for us. Okay, let's do another one. Let's do some kind of test. And maybe I want to grab the, I don't know, the, or I want to shave off the last two characters of a string. So maybe our string is foobar, and you need to get rid of ar. And, you know, it's funny. It's a very simple task, of course.

Using Copilot for Strings1:43

So maybe our string is foobar, and you need to get rid of ar. And, you know, it's funny. It's a very simple task, of course. But, yeah, sometimes, especially when you use lots of languages, you forget how to do it. So why don't we let OpenAI help with us? Shave the last two characters off of string. And notice, come on, that's really cool, amazing stuff. All right, what else could we do? We could have it write some tests for us. So let's go into the test directory, and let's see what we have here. We have an example test that uses pest php.

Auto-Writing Pest Tests2:13

So let's go into the test directory, and let's see what we have here. We have an example test that uses pest php. Stay tuned for this. We're actually going to talk more about pest in the next episode. But, yeah, maybe we could write tests for a PostController. So check this out. I could say pest feature test for a PostController, and we could let it do the job. All right, so it can create a Post. Let's do another one. It can list Posts. Let's do another one.

It can list posts. Let's do another one. It can show posts. And, yeah, that alone is pretty ridiculous. But just keep in mind, even though it feels magical, and real quick, I will hit option return to import this, even though it feels magical, it's still just predicting what the likely next character or string of characters should be. And often, it won't quite be what you want. So, for example, let's see. It can show a Post.

Reviewing Copilot Output3:01

So, for example, let's see. It can show a Post. All right, whip up a Post in the database. It's smart enough to know likely what the endpoint is to view a specific Post, and then it asserts a 200 status, and then it also asserts that the Post attributes, all of them, will be included as part of the JSON, which may or may not be what you want. But, yeah, at the very least, it'll get you started, or it might give you some ideas of where you want to go. Now, another option is to let it incrementally assist you. So I could say, okay, it shows a Post.

Incremental Assist and Triggers3:29

Now, another option is to let it incrementally assist you. So I could say, okay, it shows a Post. All right, and now I will begin to type. And notice, as I do so, it provides suggestions for me. And I saw it nine times out of ten, it is spot on. So in this case, well, given that I have a Post, when I visit a page, and again, it's smarter than I can speak. If I visit the endpoint, then, well, I expect a 200 status code. What else? I expect to see the post title on the page.

What else? I expect to see the post title on the page. And, yeah, it's funny. It's not 100%, but it's pretty close to what I would have written, which is neat. All right, let's do another one. It persists a new post in the DB. Okay, so notice here I'm not getting any suggestion. It will provide suggestions as I start typing, but otherwise, if you want to manually trigger it, on the Mac, you can press option backslash. There we go.

if you want to manually trigger it, on the Mac, you can press option backslash. There we go. So let's see what it comes up with. In this case, it creates a Post, but it doesn't persist it. It calls the make method instead. So now we have a Post model that is not saved to the database. And then it makes a POST request to this endpoint. It passes through the attributes. And, yeah, in response, we expect a 302 redirect. And then we also expect that this new Post has been persisted within the post table.

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