تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Adding ensureExists Method0:00

All right, we've got session ID based cards happening. Now let's try to be able to create cards without having to do it manually in the database. All right, so in our card model, next to the if_exist() function that we've created to query the card, we are going to make sure that we have a mechanism that ensures that there is a card existing. I think a good name for this one is going to be ensure exists.

I think a good name for this one is going to be ensure exists. And for this one, we're going to use Laravel's first or create method, return static first or create. So what we want to check for is precisely that the session ID matches the current session ID. This is the exact same check that up here, but instead of returning null or the first, it's going to create if it doesn't find the first. And just to be very clear to reiterate the need

it's going to create if it doesn't find the first. And just to be very clear to reiterate the need for these two functions, we only want to create a card when it's absolutely necessary as in when the first interaction with the card happens. But anyone visiting the site for the first time, we don't really don't want to create a card just because they loaded the page. And this is why we have an if_exist() function that the front end can use to query,

And this is why we have an if_exist() function that the front end can use to query, hey, is there a card if not no problem? And then a little bit more strict ensure exists that needs a card before doing an operation like adding an item to said card. Speaking of interacting with the card, I think it is time now to try wire up this add to card button to actually create a card in the backend with our new ensure exists function.

Creating Add-to-Cart Route1:35

to actually create a card in the backend with our new ensure exists function. So if I go in the product template, remember that each product has this form, you can see how to do comment here, that is going to do a post request to add an item to the card whenever the button here is clicked. So let's organize this action here and this endpoint. So in web.php, we have the welcome page get route.

So let's organize this action here and this endpoint. So in web.php, we have the welcome page get route. And now we want to add a post route, so route, post. And here let's say we want to hit the slash card and because we'll need to know the product that we want to add, we can use a product dynamic ID here. And we could have the logic directly here in the web.php file, but we are going to have multiple operations

Scaffolding Cart Controller2:21

in the web.php file, but we are going to have multiple operations related to cards. So let's create a card controller so we can consolidate all of this inside that file. So here it doesn't exist yet, but we are going to create a card controller class. And here we won't call it store because really it is an increment by one. So let's call that add one.

because really it is an increment by one. So let's call that add one. And maybe I can give this route a name of cards dot add one. All right, we're getting yelled at and this is fair enough because the card controller doesn't exist yet. So let's quickly scaffold one. PHP artisan make controller. And this is going to be our card controller. And we'll have an empty results here. All right, here it is.

And we'll have an empty results here. All right, here it is. Okay, and we'll create our first method. We called it add one, remember. And this is going to receive a product and have the product variable. And let's maybe start by dump and dying the product to see if we can get to that point. In our web that PHP file, I can now go import the card controller,

Wiring Form to Route3:23

In our web that PHP file, I can now go import the card controller, no more squiggly's. And we've named that route card dot add one and it's a post route. So let's try go in our product template and here use Ziggy routes to set the action to be route card dot add one. And Copilot is saving us here by reminding us that we need to pass the product

And Copilot is saving us here by reminding us that we need to pass the product since the add one method expects the product. Okay, so now in the UI, assuming this is product one, two, three, let's click on add two cards on product three and see what happens. Click and looks like it has dumped a product. And if we look at the properties, the ID is indeed three. Okay, so that's a good start.

Ensuring Cart on Add4:05

the ID is indeed three. Okay, so that's a good start. Obviously we don't want to just dump and die the product. We want to add it to the card. So we want to create a card with the ensure exists function we've created and then add an item a card item to that newly created card. Let's see if we can pull that up. Let's try that in our add one function in the card controller. Remember we need to ensure that the card exists.

Let's try that in our add one function in the card controller. Remember we need to ensure that the card exists. So let's try card equals card. We can import the model and then call the ensure exists method. And then let's dump and die the card. I'm expecting here that the card already exists. Remember we pasted the decision ID in the database. And so it should dump and die the card with an ID of one from the database. Let's give it a go.

with an ID of one from the database. Let's give it a go. Let's try again. It looks like we do have the card. Like I said, let's try click on the product with an ID of two and it should return us this time a card instance and the card should have an ID of one. Now to actually test our functionality, let's go back as an incognito window to visit that page, click on a button

let's go back as an incognito window to visit that page, click on a button and it should find a new session ID. Realize that there is no card matching and then create this new card and retrieve us the new card with the new ID. All right, new incognito user here. I will click on the sixth product and I'm hoping, whoops, wishful thinking, but this is a great sign that things are progressing.

Fixing Mass Assignment5:31

and I'm hoping, whoops, wishful thinking, but this is a great sign that things are progressing. You can see that we don't have this session ID in the fillable property to our card model. This is level protecting us from mass assignment. So let's go and add a protected fillable with this session ID property just like that. And we will try one more time. Actually, I will click on the first product. It doesn't really matter.

Actually, I will click on the first product. It doesn't really matter. What I'm hoping is it has created a new card instance and it's new. So the ID here should be three and yep, it is. And if I go in the database that had two cards, if I refresh the database, we should see a new card with the new session ID created just then. Now, of course, we've just created a card. I don't think that any new card item has been created

Now, of course, we've just created a card. I don't think that any new card item has been created and nope, it hasn't. And it makes sense. Our function is called add one, but all it does is ensure that there is a card and creates it if it doesn't exist. This is already good progress. It's actually the foundation of our card system. We can now retrieve a card if it exists or not.

It's actually the foundation of our card system. We can now retrieve a card if it exists or not. And we can make sure that a card exists before we do something like incrementing a product item inside the card. So in the next lesson, we are going to actually finish off the add one functionality. Let's see you there.

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