How to work with Resource on Laravel?

Asked

Viewed 335 times

0

My question is basically the actions/actions Store, Update and Destroy.

  • I want to create a new record in the database, I must use the verb POST to call the action store?
  • I want to update an existing database record, I must use which verb, the action will store or update?

I ask this because in the Laravel documentation "Actions Handled By Resource Controller" https://laravel.com/docs/5.4/controllers there are some verbs and action that raise this doubt as to their use.

Note: If I’m right, the update and Destroy action should be used to manipulate files in the file system. That’s right?

1 answer

3


The Resource in Laravel create methods for performing CRUD.

Methods are used to list, update, show, create and delete records.

For example, if you create a Resource for Userscontroller, you should have the methods index (get), create (get), store (post), edit (get), update (put), destroy (delete).

I want to create a new record in the database, I must use the verb POST to call the action store?

Yes, the Resource method equivalent to the "create" action is the store. The requested HTTP request method is POST.

The method create is just the interface for creating, so it’s a GET method.

I want to update an existing database record, I must use which verb, the action will store or update?

Should be update. The method update requires a request of type PUT. It is important to highlight this, because if you are using a request of the type Ajax, you will need to specify this method.

If I’m right, the update and Destroy action should be used to manipulate files in the file system. That’s right?

Not necessarily a file system. Resource actually aims to make it easier to create a CRUD, based on REST, where you have each url responsible for an action.

Usually used in Laravel update to update a record, and destroy, to remove.

  • 1

    Thank you very much for the enlightening explanation Wallace.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.