Form with has many relationship

920 Views Asked by At

What is the better way to create a model with has many relationship?

For example:

I have two models:

  • Client (id, name, ...)
  • Contact (id, type, value, description)

A client has many Contacts.

Example of create client view: http://laravel.io/bin/mGXEE

Problems:

  1. how to deal with validations?
  2. if validation fails, going back and fill in the contact with the respective errors?
1

There are 1 best solutions below

1
On

With php artisan make:request you can make a request file that will validate your information. Take a look at the documentation: https://laravel.com/docs/5.1/validation

Example:

 public function rules()
    {
        return [
            'name' => 'required|min:3|max:20',
            'lastname' => 'required|min:3|max:20',
            'adres' => 'required|min:3|max:20',
            'zip' => 'required|min:3|max:20',
            'city' => 'required|min:3|max:20'
        ];
    }

View:

 @foreach ($errors->all() as $error)
      <p>{{ $error }}</p>
 @endforeach