title: inserting in different tables from one form in Laravel 5
body: <p>Hello stackoverflow please help me to understand laravel, i am new in laravel 5. I am really a beginner in laravel. I am learning Laravel 5. Now i am making a registration page where i need to insert my data into my database.</p>
  
  <p>Here is my problems:
  1. Please correct my code, because i cant insert my data into database.
  2. Inserting my data into two tables.</p>
  
  <p>Now in my situation, i have this two tables:</p>
  
  <p><a href="https://i.stack.imgur.com/iucwN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iucwN.png" alt="enter image description here"></a></p>
  
  <p>Now, i am creating a form which has this fields:</p>
  
  <pre><code>&lt;form class="form-horizontal" action="{{ route('user_layouts.signup') }}" method="post"&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="email"&gt;Email:&lt;/label&gt;
      &lt;div class="col-sm-6"&gt;
        &lt;input type="email" class="form-control" id="email" name="email" placeholder="Email"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="pwd"&gt;Password:&lt;/label&gt;
      &lt;div class="col-sm-6"&gt; 
        &lt;input type="password" class="form-control" id="password" name="password" placeholder="Password"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="fname"&gt;First Name:&lt;/label&gt;
      &lt;div class="col-sm-6"&gt; 
        &lt;input type="text" class="form-control" id="fname" name="fname" placeholder="First Name"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="lname"&gt;Last Name:&lt;/label&gt;
      &lt;div class="col-sm-6"&gt; 
        &lt;input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="mname"&gt;Middle Name:&lt;/label&gt;
      &lt;div class="col-sm-6"&gt; 
        &lt;input type="text" class="form-control" id="mname" name="mname" placeholder="Middle Name"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="contactnum"&gt;Contact #:&lt;/label&gt;
      &lt;div class="col-sm-6"&gt; 
        &lt;input type="text" class="form-control" id="contactnum" name="contactnum" placeholder="Contact Number"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="gender"&gt;Gender:&lt;/label&gt;
      &lt;div class="col-sm-6"&gt; 
          &lt;label class="radio-inline"&gt;&lt;input type="radio" name="gender"&gt;Male&lt;/label&gt;
          &lt;label class="radio-inline"&gt;&lt;input type="radio" name="gender"&gt;Female&lt;/label&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="control-label col-sm-3" for="bday"&gt;Birthday:&lt;/label&gt;
      &lt;div class="col-sm-8"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-xs-4"&gt; 
              &lt;select class="form-control" id="month" name="month"&gt;
                  &lt;option&gt;Month&lt;/option&gt;
                  &lt;option value="Jan"&gt;Jan&lt;/option&gt;
                  &lt;option value="Feb"&gt;Feb&lt;/option&gt;
                  &lt;option value="Mar"&gt;Mar&lt;/option&gt;
                  &lt;option value="Apr"&gt;Apr&lt;/option&gt;
                  &lt;option value="May"&gt;May&lt;/option&gt;
                  &lt;option value="Jun"&gt;Jun&lt;/option&gt;
                  &lt;option value="Jul"&gt;Jul&lt;/option&gt;
                  &lt;option value="Aug"&gt;Aug&lt;/option&gt;
                  &lt;option value="Sep"&gt;Sep&lt;/option&gt;
                  &lt;option value="Oct"&gt;Oct&lt;/option&gt;
                  &lt;option value="Nov"&gt;Nov&lt;/option&gt;
                  &lt;option value="Dec"&gt;Dec&lt;/option&gt;
              &lt;/select&gt;
              &lt;/div&gt;
  
              &lt;div class="col-xs-4"&gt;                      
              &lt;select class="form-control" id="day" name="day"&gt;
                  &lt;option value="day"&gt;Day&lt;/option&gt;
                  @for ($i = 0; $i &lt;= 31; $i++)
                      &lt;option value="{{ $i }}"&gt;{{ $i }}&lt;/option&gt;
                  @endfor
              &lt;/select&gt;
              &lt;/div&gt;
  
              &lt;div class="col-xs-4"&gt;
              &lt;select class="form-control" id="year" name="year"&gt;
                  &lt;option&gt;Year&lt;/option&gt;                                   
                  &lt;option value="1992"&gt;1992&lt;/option&gt;
                  &lt;option value="1993"&gt;1993&lt;/option&gt;
                  &lt;option value="1994"&gt;1994&lt;/option&gt;
                  &lt;option value="1995"&gt;1995&lt;/option&gt;                                      
              &lt;/select&gt;
              &lt;/div&gt;
          &lt;/div&gt;                  
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt; 
      &lt;div class="col-sm-offset-3 col-sm-10"&gt;
        &lt;button type="submit" class="btn btn-success"&gt;Submit&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    {{ csrf_field() }}
  &lt;/form&gt;
  </code></pre>
  
  <p>now here is my routes:</p>
  
  <pre><code>&lt;?php
  
  /*
  |--------------------------------------------------------------------------
  | Web Routes
  |--------------------------------------------------------------------------
  |
  | Here is where you can register web routes for your application. These
  | routes are loaded by the RouteServiceProvider within a group which
  | contains the "web" middleware group. Now create something great!
  |
  */
  
  Route::get('/', function () {
      return view('shop_layouts.index');
  });
  
  Route::get('/signup', [
      'uses'  =&gt; 'UserController@getSignup',
      'as'    =&gt; 'user_layouts.signup'
  ]);
  
  Route::post('/signup', [
      'uses'  =&gt; 'UserController@postSignup',
      'as'    =&gt; 'user_layouts.signup'
  ]);
  </code></pre>
  
  <p>Here is my controller:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  
  class UserController extends Controller
  {
      public function getSignup() {
          return view('user_layouts.signup');
      }
  
      public function postSignup(Request $request) {
          $this-&gt;validate($request, [
              'email' =&gt; 'email|required|unique:tbl_users',
              'password' =&gt; 'required|min:24',
              'fname' =&gt; 'required',
              'lname' =&gt; 'required',
              'mname' =&gt; 'required',
              'contactnum' =&gt; 'required',
              'gender' =&gt; 'required',
              'month' =&gt; 'required',
              'day' =&gt; 'required',
              'year' =&gt; 'required'
          ]);
  
          $user = new User([
              'email' =&gt; $request-&gt;input('email'),
              'password' =&gt; bcrypt($request-&gt;input('password')),
          ]);
          $user-&gt;save();
  
          $customer = new Customer([   
              'fname' =&gt; $request-&gt;input('fname'),
              'lname' =&gt; $request-&gt;input('lname'),
              'mname' =&gt; $request-&gt;input('mname'),
              'contactnum' =&gt; $request-&gt;input('contactnum'),
              'gender' =&gt; $request-&gt;input('gender'),
              'lname' =&gt; $request-&gt;input('lname'),
          ]);
          $customer-&gt;save();
      }
  
  }
  </code></pre>
  
  <p>and lastly my two models:</p>
  
  <p>Customer Model</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Customer extends Model
  {
      protected $fillable = [
          'fname', 'lname','mname','contactnum','gender'
      ];
  
      public function user() {
          return $this-&gt;belongsTo(User::class);
      }
  }
  </code></pre>
  
  <p>User Model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Database\Eloquent\Model;
  
  class User extends Authenticatable
  {
      use Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'email', 'password'
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      public function customer() {
          return $this-&gt;hasOne(Customer::class);  
      }
  }
  </code></pre>
  
  <p>Here is my migration:
  Customer:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class TblCustomers extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('tbl_customers', function (Blueprint $table){
              $table-&gt;increments('customer_id');
              $table-&gt;integer('user_id')-&gt;unsigned();
              $table-&gt;string('fname');
              $table-&gt;string('lname');
              $table-&gt;string('mname');
              $table-&gt;string('contact_no');
              $table-&gt;string('gender');
              $table-&gt;date('birthdate');
              $table-&gt;timestamps();                        
          });
  
          Schema::table('tbl_customers', function (Blueprint $table){
              $table-&gt;foreign('user_id')-&gt;references('user_id')-&gt;on('tbl_users')-&gt;onDelete('cascade');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::drop('tbl_customers');
      }
  }
  </code></pre>
  
  <p>User:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateUserTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('tbl_users', function (Blueprint $table){
              $table-&gt;increments('user_id');
              $table-&gt;string('email')-&gt;unique();
              $table-&gt;string('password');
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::drop('tbl_users');
      }
  }
  </code></pre>