laravel使用另一个表中的主键作为另一个表

时间:2017-05-01 09:54:39

标签: laravel-5

我有两个表车辆表和应用程序表,我需要当用户创建应用程序时,vehicle_id会自动插入到应用程序表中,以便我可以跟踪用户申请的车辆...我创建了两个表之间的关系如下:

  

在我的应用程序模型中



 
 public function vehicle(){
    return $this->belongsTo('Martin\Models\Vehicle');
  }




  

在我的车型中



public function application(){

return  $this->hasMany('Martin\Models\Application');
}




我想要插入vehicle_id的车辆应用程序的迁移如下:



  public function up()
    {
      Schema::create('applications', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('user_id')->unsigned()->nullable();
          $table->integer('admin_id')->unsigned()->nullable();
          $table->integer('vehicle_id')->unsigned()->nullable();
          $table->integer('application_id')->unsigned()->nullable();
          $table->string('acronym');
          $table->string('destination');
          $table->integer('Number_of_days_hired');
          $table->string('vehicle_registration_number');
          $table->timestamp('departure_date_and_time');
          $table->boolean('approved')->unsigned()->nullable();
          $table->timestamp("created_at")->useCurrent();
          $table->timestamp("updated_at")->useCurrent();
          $table->foreign('user_id')
          ->references('id')
          ->on('users')
          ->onDelete('cascade');
          $table->foreign('admin_id')
          ->references('id')
          ->on('admins')
          ->onDelete('cascade');
          });
          $table->foreign('vehicle_id')
          ->references('id')
          ->on('vehicles')
          ->onDelete('cascade');
          });
    }




如何将值输入vehicle_id列?



Application::create([
            'user_id'=>Auth::user()->id,
						'vehicle_id'=>,
            'acronym'=>$request->input('acronym'),
            'destination'=>$request->input('destination'),




我的申请表格中有这个



  <form class="form-vertical" role="form" action="{{route('application.make',['id'=>Auth::user()->id])}}" method="POST">
                                               {{csrf_field()}}
                                 <input type="hidden" name="vehicle_id" value="{{Vehicle::with('application')->id}}">
                                  <div class="row">
                                    <div class="col-md-6">
                                      <div class="form-group label-floating {{$errors->has('acronym') ? ' has-error' : ''}}">
                                        <label class="control-label">Group Acronym</label>
                                        <select name="acronym" class="form-control">
                                           <option>Select Group Acronym</option><!--selected by default-->
                                           @foreach($users as $user)
                                               <option value="{{$user->acronym}}">
                                                   {{$user->description}}
                                               </option>
                                           @endforeach
                                      </select>
                                @if($errors->has('acronym'))
                                     <span class="help-block">{{$errors->first('acronym')}}</span>
                                @endif()

                                    </div>
                                    </div>

                                    <div class="col-sm-6">
                                          <div class="form-group label-floating {{$errors->has('destination') ? ' has-error' : ''}}">
                                            <label class="control-label">Destination</label>
                                            <input type="text" name="destination" class="form-control" required>
                                            @if($errors->has('destination'))
                                                 <span class="help-block">{{$errors->first('destination')}}</span>
                                            @endif()

                                          </div>
                                          </div>

                                </div><!--ends first -row-->



                                                <div class="row">

                                                  <div class="col-sm-4">
                                                    <div class="form-group label-floating {{$errors->has('Number_of_days_hired') ? ' has-error' : ''}}">
                                                      <label class="control-label">Number Of Days Hired</label>
                                              <select  name="Number_of_days_hired" class="form-control">
                                                <option value="1">1</option>
                                                <option value="2">2</option>
                                                <option value="3">3</option>
                                                <option value="4">4</option>
                                                <option value="5">5</option>
                                              </select>

                                              @if($errors->has('Number_of_days_hired'))
                                                   <span class="help-block">{{$errors->first('Number_of_days_hired')}}</span>
                                              @endif()

                                                  </div>
                                                        </div>

                                                  <div class="col-sm-4">
                                                        <div class="form-group label-floating {{$errors->has('vehicle_registration_number') ? ' has-error' : ''}}">
                                                          <label class="control-label">Vehicle</label>
                                                          <select name="vehicle_registration_number" class="form-control">
                                                             <option>Select Vehicle</option><!--selected by default-->
                                                             @foreach($vehicles as $vehicle)
                                                                 <option value="{{$vehicle->vehicle_registration_number}}">
                                                                     {{ $vehicle->description }}
                                                                 </option>
                                                             @endforeach
                                                        </select>
                                                  @if($errors->has('vehicle_registration_number'))
                                                       <span class="help-block">{{$errors->first('vehicle_registration_number')}}</span>
                                                  @endif()

                                                      </div>
                                                        </div>



                                                              <div class="col-sm-4">
                                                                    <div class="form-group label-floating {{$errors->has('departure_time') ? ' has-error' : ''}}">
                                                                      <label class="control-label">Departure Date and Time</label>
                                                                      <input type="datetime-local"  name="departure_date_and_time" class="form-control" required>
                                                                      @if($errors->has('departure_time'))
                                                                           <span class="help-block">{{$errors->first('departure_time')}}</span>
                                                                      @endif()

                                                                    </div>
                                                                    </div>



                                                </div><!--ends second row-->
&#13;
&#13;
&#13;

我的应用程序是一个传输管理系统或公共汽车预订系统,当用户提出申请时,他的传输被保留

0 个答案:

没有答案