Advertisement
LTroya

Migration Example

Apr 7th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. // Matches Tables
  2. class CreateMatchesTable extends Migration
  3. {
  4.     /**
  5.      * Run the migrations.
  6.      *
  7.      * @return void
  8.      */
  9.     public function up()
  10.     {
  11.         Schema::create('matches', function (Blueprint $table) {
  12.             $table->increments('id');
  13.             $table->integer('visitor_id')->references('id')->on('teams');
  14.             $table->integer('local_id')->references('id')->on('teams');
  15.             $table->timestamp('match_date');
  16.             $table->timestamps();
  17.         });
  18.     }
  19.  
  20.     /**
  21.      * Reverse the migrations.
  22.      *
  23.      * @return void
  24.      */
  25.     public function down()
  26.     {
  27.         Schema::dropIfExists('matches');
  28.     }
  29. }
  30.  
  31.  
  32. // Team Table
  33. class CreateTeamsTable extends Migration
  34. {
  35.     /**
  36.      * Run the migrations.
  37.      *
  38.      * @return void
  39.      */
  40.     public function up()
  41.     {
  42.         Schema::create('teams', function (Blueprint $table) {
  43.             $table->increments('id');
  44.             $table->string('name')->unique();
  45.             $table->string('email')->unique();
  46.             $table->string('phone');
  47.             $table->string('website');
  48.             $table->timestamps();
  49.         });
  50.     }
  51.  
  52.     /**
  53.      * Reverse the migrations.
  54.      *
  55.      * @return void
  56.      */
  57.     public function down()
  58.     {
  59.         Schema::dropIfExists('teams');
  60.     }
  61. }
  62.  
  63.  
  64. // Match Model
  65. class Match extends Model
  66. {
  67.     protected $fillable = ['visitor_id', 'local_id', 'match_date'];
  68.  
  69.     public function team()
  70.     {
  71.         return $this->belongsTo(\App\Models\Team::class);
  72.     }
  73. }
  74.  
  75.  
  76. // Team Model
  77. class Team extends Model
  78. {
  79.     protected $fillable = ['name', 'email', 'phone', 'website'];
  80.  
  81.     public function local_matches()
  82.     {
  83.         return $this->hasMany(\App\Models\Match::class, 'local_id', 'id');
  84.     }
  85.  
  86.     public function visitor_matches()
  87.     {
  88.         return $this->hasMany(\App\Models\Match::class, 'visitor_id', 'id');
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement