lundi 1 mai 2017

laravel using a primary key in another table as input in another table

I have two tables Vehicles table and applications table, I need when a user makes an application, the vehicle_id is automatically inserted into the applications table so that I can track the vehicle the user has applied for... I have created the relationship between the two tables as follows:

In my application model

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

In my Vehicle model

public function application(){

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

my migrations for vehicle applications where I want the vehicle_id inserted are as follows:

  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');
          });
    }

how do I get the values into the vehicle_id column?

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


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire