How to work with resource in Laravel?

0

My question basically comes down to the actions / actions Store, Update and Destroy.

  • I want to create a new record in the database, should I use the POST verb to call the action store?
  • I want to update an existing registry in bank, should I use that verb, the action will be store or update?

I wonder why in the Laravel documentation "Actions Handled By Resource Controller" link there are some verbs and action that raise this questions about its use.

Note: If I am right, action update and destroy should be used to manipulate files in the file system. Is that right?

    
asked by anonymous 27.02.2017 / 20:59

1 answer

3

The resource in Laravel creates methods for performing CRUD.

Methods are used to list, update, display, 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, should I use the POST verb to call the action store?

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

The create method is just the interface for creation, so it's a GET method.

  

I want to update an existing registry in a database, should I use that verb, the action will be store or update?

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

  

If I am right, action update and destroy should be used to manipulate files in the file system. Is that right?

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

Generally, you use Laravel update to update a record, and destroy , to remove.

    
28.02.2017 / 01:02