How to be more Laravel in CRUD application?

This is a question about how to create a better application with awesome Laravel. I want to create a CRUD application with multiple forms with user rights.

More details :

  • View . I have 3 forms, for example call him cat

    , turtle

    and dog

    . Most importantly, they have a different number of fields. Here are the forms

  • controller . After the user has filled out the form and pressed the button save

    , the controller is in the game. We all know a thinner controller is better. But I use a construction like this:

    switch($type)
    {
        case '1':
            //form validation -> move to model
            //if all have passed that insert into database
            //Show user a message
            break;
    
        case '2':
            //form validation -> move to model
            //if all have passed that insert into database
            //Show user a message
            break;
    //.....
    
        default:
            //show message
            break;
    
    }
    
          

    If the variable type

    is equal to the type of the form that was filled in by the user. But as you can see the controller is quite big and I don't like it. What's the best way to do this?

  • Model . I have 4 tables, dogs_data

    , turtle_data

    , cat_data

    , where I keep all the data from the forms and pets_data

    where to store metadata about the various animals above. How it works? After the validation has passed, insert data into one of these tables and get the ID of this insert. Then create a record in pets_data

    and paste the identifier as pet_id

    , and also paste table_type

    that you need to get information from the database. I think this is a very strange way of working with a database. Here is the table visualization.

Another problem is showing data. For example, a user does not have rights to edit data or delete records, but an administrator can delete, update, and see a lot of data from the database. Should I create another controller like AdminController and write another method there that only displays information for the admin?

My application is now:

  • WebSiteController

    - website display
  • AdminController

    - admin page, there is a method that generates a special page for viewing pets.
  • UserController

    - user control panel, there is also a method that generates a page for viewing pets, but specifically for the user.

And I am thinking to create something like this

  • AuthController

    - special for login \ registration \ activation \ etc
  • PetsController

    - create \ delete \ update \ etc, and also view in two versions: for user

    and admin

    . And the idea is to create a method getShowPets($pet_id)

    (only the id of is important pets_data

    , not pets_id ), then get the user's permission and generate the page.
  • And so on, I want to rewrite my application using the DRY rule

    Therefore, I will be very happy if you can give me good advice on this project.

+3


source to share


1 answer


It looks like you are setting up a polymorphic relationship, in which case I would do all the individual value so that you have a model for Cat, Dog, Turtle and PetData. You will have controllers for CatController, DogController and TurtleController. And you will have a cat shape, a dog shape, and a turtle shape, each of which also contains inputs for the data you need for your pet_info table.

To create a polymorphic relationship, your pet_data table will require some changes. Just change the table column to pet_type

. Laravel expects a specific name for this column, and its contents will be the model name, not your table name.

Setting up the models for them is very easy, I'll make PetData and Dog to start with.

class Dog extends Eloquent 
{
    protected $table = 'dogs_data';
    protected $timestamps = false;

    public function petData()
    {
        return $this->morphMany('PetData', 'pet');
    }
}

class PetData extends Eloquent
{
    protected $timestamps = false;

    public function pet()
    {
        return $this->morphTo();
    }
}

      

Read more about this here ... http://laravel.com/docs/eloquent#polymorphic-relations



The idea of ​​having separate models / controllers for everything may seem like a lot of work, but it's a long way to go when trying to save or add additional animals to an application because you rarely have to change production code, taking away the opportunity to introduce more bugs when trying to add improvements to your site.

It is now very easy to store pet and related animal data without worrying about pet_id and pet_type in the pet_data table, Laravel will take care of that for you. A function on your dog controller might look like this:

class DogController extends BaseController
{
    public function save()
    {
        $dog = new Dog;
        $dog->name = Input::get('name');
        $dog->age = Input::get('age');
        $dog->save();

        $pet_data = new PetData;
        $pet_data->color = Input::get('color');
        $dog->petData()->save($pet_data);
    }
}

      

As far as creating another controller for admins, I would say yes, do it. It never hurts to save parts of your site that you think are different in different files. This not only helps the organization, but again, separating concerns that you should probably read more about.

There are some fantastic third-party role management options available as well. I've used it zizaco/entrust

before and found it to be very easy to manage. It can make your life a lot easier when trying to control who can do what your web application has.

+2


source







All Articles