What is CRUD, and how can it make you a better programmer?

Coding (PHP 8)


Becoming an expert by learning by doing is the best way to learn
 
/img/blog/what-are-crud-operations-in-php.jpeg

The following story started many years ago.

When a wannabe web developer was spending his weekends in coffee shops around London with friends learning how to code.

One of the questions we were asking ourselves was, what do I code?

How can I learn to code if I don’t know how to practice?

The solution is pretty simple and it’s enclosed in an acronymous: CRUD.

In this article, I’ll share with you how CRUD made me a better programmer and how it can help you (no matter your current experience).

 

But first, the importance of practice

In Malcolm Gladwell’s book Outliers, he explains that you can become an expert in every subject.

But, to do so we need at least 10.000 hours.

That’s a lot!

To get things worse is that he meant 10.000 hours of practice.

As someone who already did mine, I can tell you that 10.000 hours spent watching tutorials on YouTube or reading books do not add up.

Those surely help, but the best way to improve faster is to actually do.

By doing you will find issues or things that do not work.

Then you will work on fixing these issues.

That is the part that will improve your skill and increase your problem-solving too.

So, what can you actually do if you don’t know where to start?

CRUD to the rescue!

 

 

 

 

What does CRUD stand for?

Let’s start with the basics then we will see the practical example.

What is C.R.U.D.?

C.R.U.D. are the initials of the basic operations that can be done on a model.

They are: Create, Read, Updated, and Delete.

Two of the basics exercise when we start to get involved in coding is how to create a to-do application or a blog.

The models in these cases will be the tasks or articles.

Basically, a model is an object we are acting upon.

The most basic exercise is to write some code that does these 4 operations on these objects.

 

CRUD operations in detail

CREATE: when writing a blog or a to-do application the most crucial part is to be able to write in a way that is persistent.

As I am writing this article these bytes of information are being stored in a database and I am going to be able to retrieve them in the future.

READ: After the article has been created and it is stored in the database, it should now be available for you, as a reader, to get and ‘read’ it.

We do this by querying a database or looking up a JSON file or wherever this data is saved.

UPDATE: When I write an article, on the first iteration, usually I make some grammar mistakes, or I decide to change some details afterward.

Updating the object must be considered another fundamental part of the operation you can do in software applications.

DELETE: Deleting is the last of the main operations you can perform on an object.

Imagine you complete the task on your to-do or you want to delete an article that is no longer relevant.

You need to be able to remove it from where it is stored if you want.

 

PRO tip: sometimes it is better not to really delete objects.

let's say you finished all the chores listed in the to-do app, then want to count how many chores you completed on a given day.

To solve this problem what we actually use is a technique called soft-delete

Here is an article from Brent Ozar on how to implement soft delete.

 

CRUD vs REST

Here is another acronym for you:

R.E.S.T. stands for REpresentational State Transfer.

Consider it a standard of communication for computers on the web.

RESTful systems, are stateless and separate the concerns of the client and server.

I will write about this in more detail in a specific article soon so be sure to subscribe to my mail letter.

REST is based on 4 or more HTTP verbs.

These verbs are used differently depending on the type of request we are doing.

 

POST a post request is used when creating a new object.

GET is when we want to fetch data from our source, used when reading.

PUT or PATCH is when we want to update some details instead of creating a brand-new article.

DELETE is when we remove the selected data.

 

An easy CRUD example for you

Photo by Glenn Carstens-Peters on Unsplash

let’s make some CRUD requests we can use as a reference while learning how to code.

To do so we are going to create a blog.

The one below will be an agnostic example, good for every programming language.

I you want to read something specifically for PHP visit the Basics of PHP.

 

Creating a Blog Post.

First thing first, we need to create our blog post.

That would be a POST request to our blog endpoint

 

 

Request:

POST http://www.myblog.com/blog
Body: 201 (CREATED)

{
  "article": {
    "id": 1,
    "title": "What is CRUD, and how can it make you a better prograrem?",
    "author": 'Nico'   
  },
 'created_at' : '1676024223' 
}
 

 

The body is the content we are actually sending and that needs to be saved.

 

Updating our Blog Post.

What if I realize that I made a mistake?

let’s pretend I wrote ‘prograrem’ instead of ‘programmer’?

In this case, I don’t want to send the whole thing all over again, it is time-consuming and it will use plenty of resources.

There are other verbs that we can use instead

They are PUT and PATCH

let's test it:

 

 

Request:

PATCH http://www.myblog.com/blog/1
Body: 200 (OK)

{
  "article": {
    "title": "What is CRUD, and how can it make you a better programmer?",
  }
}
 

 

This time we had to specify what article we needed to update.

We’ve done so via the id in the URL.

Also, we haven’t sent the whole body again but only the field we wanted to update.

This is way faster and more performant.

Now that we have uploaded our article and checked that there is no error it is time to read the content

 

Reading Blog Posts.

Here we have 2 choices here.

Do we want to get all the articles or just a specific one?

In case we want to get the whole collection we can be generic.

 

Request:

GET http://www.myblog.com/blog
Body: 200 (OK)

{
  "article": {
    "id": 1,
    "title": "What is CRUD, and how can it make you a better programmer?",
    "author": 'Nico'   
  },
 'created_at' : '1676024223' 
},
{
  "article": {
    "id": 2,
    "title": "How RESTful API changed my life",
    "author": 'John'   
  },
 'created_at' : '1676027263' 
},
{
  "article": {
    "id": 3,
    "title": "Spaghetti vs Gnocchi: the definitive battle",
    "author": 'Gordon'   
  },
 'created_at' : '1676029842' 
}
 

 

Pretty simple, right?

Once the blog post is up and running you might find yourself with hundreds or thousands of objects.

To solve this issue you can use a technique called pagination

Here is a tutorial on how to create a pagination from scratch in PHP.

What if you only want to read a single article?

As per the update request, to read a single article we need to specify its ID

 

 

Request:

GET http://www.myblog.com/blog/1
Body: 200 (OK)

{
  "article": {
    "id": 1,
    "title": "What is CRUD, and how can it make you a better programmer?",
    "author": 'Nico'   
  },
 'created_at' : '1676024223' 
}
 

 

Deleting the Post

The last one of our operations is the deletion:

It works similarly to the update and individual read

 

 

Request:

DELETE http://www.myblog.com/blog/1
Body: 204 (NO CONTENT)

{}
 

 

Here you have it, the 4 operations in a useful post.

To simulate this on your local try to use REST service as Postman.

I wrote how to use postman in this article.

 

Conclusion

CRUD and REST verbs are easy but quite complex if this is the first time you came across this concept.

In this blog, I tried my best to show you how can you perform all the main operations on a domain.

In the next article, I’ll show more examples of how I use RESTful API to rebuild my own website.

To learn more about this topic subscribe to my newsletter.

Thanks for reading.

 
 
If you like this content and you are hungry for some more join the Facebook's community in which we share info and news just like this one!

Other posts that might interest you

Coding (PHP 8) Jan 31, 2023

How Can You Get Coworkers to Review Your Code?

See details
Coding (PHP 8) Mar 31, 2023

How to use API in PHP (with Example)

See details
Coding (PHP 8) Apr 11, 2023

What’s new in PHP 8.2? (7 features for you)

See details
Get my free books' review to improve your skill now!
I'll do myself