How to do CRUD for the model in Laravel?

0

We can do in controller method save() o find($id) o update() and create() . How could I do that Model have this responsibility ?

    
asked by anonymous 06.08.2017 / 10:02

1 answer

1

Your question is a bit vague, but I think I understood your question.

  

How could you do that Model has this responsibility?

The model already has this responsibility. It is Eloquent that knows how to search with find , create a new instance with create and update it with save or update (has delete also né).

The responsibility of Controllers methods is different. They should orchestrate the various steps of interacting with some CRUD command. They are usually composed of:

  • Authorization to perform the operation
  • Order Validation
  • Running the CRUD command
  • Sending the response (view, json, etc.)
  • For each of these steps, Laravel presents several tools:

  • Authorization: Gates, Policies, and% method of controller%
  • Validation: Validators with Validation Rules and Custom Validation
  • CRUD: Eloquent model or QueryBuilder and DB Facade
  • Response: Views with Blade or automatic formatting of JSONs
  • Of course this is a simplified view, but the idea is to make it clear that Controller's role is to orchestrate these steps and Model's role is just one of them.

        
    06.08.2017 / 18:53