[TUT] 7 - Updating & Deleting / Wrapping up - Laravel Tutorial Series
#1
If you didn't catch the last tutorial, you can view it here


~~~~ Hey, everyone! ~~~~

It has been some time since the previous tutorial, so let's do a quick recap.

In the last tutorial, we utilised Laravel's Eloquent ORM; we dealt with form->route processes, requests & responses,
as well as actually inserting data into the database, and retrieving it. I mentioned towards the end that
we've now covered the 'C' and the 'R' in CRUD. What's left is to demonstrate the 'U' (Updating), and
the 'D' (Deleting).

Hopefully we can fit that all into this one last tutorial and wrap things up.

So let's get started...




Deleting Records

Do you remember how, in the previous tutorial, we were able to access a `Person` object using Laravel's ORM?

Well, we'll use the same method to delete records too. This way we don't actually need to write any Queries.

So start with, let's provide a visual button to delete a particular record.


Currently ...


[Image: def1418e066b4bdc82fdc4c522b85d7f.png]


This is how our records are being displayed... so let's generate a form and a delete button next to it.


[Image: 4bf81c9c5977484183f78a49b1334605.png]


Now you may be thinking: what on earth is that?

Worry not, it's an alternative for us to generate forms using Laravel, instead of doing it the other way.

Let me quickly show you what that becomes in the source:


[Image: 5aa51143bf2c4e298435e8ad75ed6d77.png]


So, we can see that it generates a form, with a method of type post...
we've set the route to people.destroy and in the source it comes out as
http://localhost:8000/people/1 ... how?

Well.

We're going to be creating the route, and the alias we'll give to it will be "people.destroy" ... you'll see.
Next, we also see an input of type "hidden", which contains a value of "DELETE" ...
This is because you can only use "GET" and "POST" in the form, by default.

What laravel is doing, is essentially tricking the server to make the form send as a DELETE request.

Which ultimately hits the "people" route as a DELETE request, forwarding it to our destroy() function.


But this won't work just yet. You'll probably get an error saying that the Form class cannot be found, or something.


Follow the instructions in this picture:


[Image: 0c136310ae2d46f895a9a05033e698a4.png]


(https://laravelcollective.com/docs/5.2/html)


And that should now be working for you.


Basically they deprecated something and you need to require the new blablabla in order for it to work.
You can read more about it online.


Anyway...


Let's make our delete route:

(\routes\web.php)

[Image: db896be89d4748509d72212dabfa7af3.png]



Let's also make the destroy function:

(\Controllers\PeopleController.php)

[Image: 65e0636446b54a4dab38b0d9a47bdeb1.png]


Done.

Now, refresh, and we get this:


[Image: 89efdef305e5400198d76fa2a9104736.png]



If you click on the delete button, we'll get a page that just dumps the `id` of that record:


[Image: f0bc61ebb9694afea2dcb70c54f077a2.png]


If you don't know by now (which you should, btw,) it's doing that because that's what we told the destroy function
in our `PeopleController` to do.

It's just to make sure we're receiving the request; next, we'll delete it.





Actually deleting the record

There are a few ways we could do this:

Method 1)


[Image: 1678fc1e1a10469fb1a98bf71e7c75d2.png]

or

[Image: a1e563a392b84f3eb0f4f2dd9335699c.png]

^ same thing


Method 2)

[Image: 95585130d8f6401a99a8d940857e052c.png]



And to save the best for last:

Method 3)


[Image: 79f76b02748f4d73a250479a9bf1ca9a.png]


You may be wondering how this is happening...

Let me try to explain real quick.


So. The form submits a request to the server, passing in an id as a parameter:

[Image: d610d7b37b024cc783b91e64d5f1deed.png]




The correct route is located, and it accepts the request because it (the request) carries
the necessary amount of paremters (in this case, just one)

[Image: e1db95cdc0de485cb20961293a5ba05b.png]



Now, this is where the magic happens. Laravel automatically casts $id to a person object, by
locating a record in the database (using its Eloquent ORM) with an ID that matches the value in
the passed-in parameter ($id) ... get it?

By specifying that we're expecting a Person for a parameter, it will use the value to search
by ID, and thus, it renders $id as an Eloquen Person object, allowing us to delete our record.

[Image: 64fca842a41d466aa6f0f198632bc7f7.png]





Updating

The last thing we need to cover, in CRUD.

First thing's first. We need to add a button that takes us to a page where we're able to edit the given item.



[Image: d0439ab1345e445e8119afd67f6348c5.png]



Which results in


[Image: 4314e32810de49d78c4287402a093a53.png]



As you can probably tell from the html, we've surrounded the button by an anchor tag which takes the user to
a people/edit/$ page, $ being the id of the selected item.

So we need to create this end-point.

In the web.php file:

Route::get('/people/show/{person}', '[email protected]')

Now let's create the function:

In the PeopleController.php file:



[Image: 37f3b967c0a5477297a13510216e0b8f.png]


That will send the Person object (the one we're editing) to a view called edit.blade.php
which we haven't made yet, so let's make that.

It should be in the resources/views/people/ directory...



[Image: 34746c7becbf4f0a8ae28ce16be651a9.png]


A quick and dirty template. It's just a HTML skeleton with an h3 title,
informing the user that they're editing the profile of so and so.

Then we open a form, of type PATCH (because we'll be sending an update request this time around)...
we route it to a route with the alias of "people.store" (which we haven't done yet), whilst passing in
the person's id as a parameter, pretty routine.

We provide some inputs, with appropriate names, and their values have been set to the current ones.

Finally, there's a save button, and the form is closed.

This is what it looks like on the browser:


[Image: dfa3bd6cbacb45b1856aac62f8b50ef1.png]


Not too fancy a design; but then again, that's not what we're focused on, here.



So, if we hit submit now, we'll get an error, telling us that there's no such route with that name...

So we need to create the endpoint in our web.php file.



[Image: 5e8f06a512354cc58b84f348a23ea4cd.png]



And now we need to create the function in our controller, according to what we've routed it to ('store' in this case)



[Image: 6390a7bd267f4e8ca2be0957e9a9d168.png]



Pretty straightforward.

We take the parameter as an Eloquent Person object. We set the name and email fields to the ones
passed in through the form (stored in request())... we then save and redirect back to our people page.





Testing


I've deleted everything, let's just test that everything is working as it should.


1) Creating a record

(http://localhost:8000/people)

[Image: ff65ff923fbc4ba5bfd0baef88f2e0cd.png]



2) Retrieving a record  (also proves the creation of it, saves me having to show the DB)

[Image: 459e02ec6ffd4efbbfdd43e07ffc3fb4.png]



3) Updating a record (after clicking edit on it and being taken to that page)

[Image: ea30083ca78e457186cf5df7dd2c1871.png]

Result:

[Image: fb049494d4bf42aabf734b55cd504d42.png]




4) Deleting a record


[Image: 5103f94df97b4243b680e383cbdb03e4.png]

Result:

[Image: e30b93fc2d174a1ebc1c586681ef1306.png]





Conclusion


In this series, we managed to build a CRUD RESTful application using Laravel in what? 7 tutorials?.

While I'm hoping that most of the content was clear and concise, I have no doubt that there may be some readers
who were confused by some parts, or maybe have some questions about a specific part or section in the series.

For that reason, the comment section is open for any suggestions, feedback, questions, and whatever else.

Please try to be respectful and contributing. Don't use it as an ego platform for yourself, and don't pick
on those who are trying to learn, because there was a time you didn't know anything too, you nut.

Other than that, thanks for reading; I hope you enjoyed it as much as  I (kinda) enjoyed typing it up ¬_¬ ...

Remember to be consistent and practice regularly in different areas, challenge yourself and become fluent.

So yeah... that wraps this up for us, kinda sad :/ It's like the end of an era, the separation of a family,
the ruthless severance of...


jk, have a nice day, chomp. Hat Tip


#2
thats high quality well done
Reply
#3
(04-24-2017, 11:17 AM)Dozy Van Wrote: thats high quality well done

A wild reader appears!

Hehe, thanks, Dozy Van Houten.
Reply
#4
(04-25-2017, 05:33 PM)Baredee Wrote:
(04-24-2017, 11:17 AM)Dozy Van Wrote: thats high quality well done

A wild reader appears!

Hehe, thanks, Dozy Van Houten.

Is Dozy called like that cause then Ill guess we have a other dutchy overhere!
Reply
#5
(05-11-2017, 09:26 PM)Coddr Wrote:
(04-25-2017, 05:33 PM)Baredee Wrote: A wild reader appears!

Hehe, thanks, Dozy Van Houten.

Is Dozy called like that cause then Ill guess we have a other dutchy overhere!

No clue lol,

got it from the book 'The fault in our stars' by John Green - and yeah he is dutch (the character, dunno about Dozy Tongue Out)
Reply
#6
Thanks man, I still have to start following it, but it looks very good man. Clearly explained.
Reply
#7
(05-12-2017, 10:22 AM)Bish0pQ Wrote: Thanks man, I still have to start following it, but it looks very good man. Clearly explained.

My pleasure ^,^
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
[TUT] 6 - Form->Database - Laravel Tutorial Series Baredee 6 2,901 03-30-2017, 12:00 AM
Last Post: Baredee
[TUT] 5 - Migrations & Schemas - Laravel Tutorial Series Baredee 0 735 03-01-2017, 06:46 PM
Last Post: Baredee
[TUT] 4 - Models & Controllers - Laravel Tutorial Series Baredee 0 807 02-28-2017, 06:28 PM
Last Post: Baredee
[TUT] 3 - Artisan / Routes / Blade - Laravel Tutorial Series Baredee 0 722 02-27-2017, 03:31 PM
Last Post: Baredee
[TUT] 2 - Installation - Laravel Tutorial Series Baredee 0 740 02-27-2017, 03:28 PM
Last Post: Baredee

Forum Jump:


Users browsing this thread: 1 Guest(s)



Powered By MyBB, © 2002-2018 MyBB Group.