Last active
December 9, 2017 04:03
-
-
Save burlresearch/0ccfcb79697db4388b960f5fd66ce950 to your computer and use it in GitHub Desktop.
Laravel relationships with multiple linking tables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Document extends Model | |
{ | |
protected $guarded = ['id']; | |
public $timestamps = false; | |
public function fileLocations() | |
{ | |
return $this->hasMany(FileLocation::class, 'foreignId', 'documentId'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class File extends Model | |
{ | |
protected $guarded = ['id']; | |
public $timestamps = false; | |
public function fileUpload() | |
{ | |
return $this->hasOne(FileUploadLocation::class, 'fileId'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class FileLocation extends Model | |
{ | |
protected $primaryKey = 'foreignId'; | |
protected $guarded = ['foreignId']; | |
public $timestamps = false; | |
public function document() | |
{ | |
return $this->belongsTo(Document::class, 'foreignId', 'documentId'); | |
} | |
public function fileUpload() | |
{ | |
return $this->hasOne(FileUploadLocation::class, 'locationId', 'id'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class FileUploadLocation extends Model | |
{ | |
protected $primaryKey = 'locationId'; | |
protected $guarded = ['locationId']; | |
public $timestamps = false; | |
public function fileLocation() | |
{ | |
return $this->hasOne(FileLocation::class, 'id', 'locationId'); | |
} | |
public function file() | |
{ | |
return $this->hasOne(File::class, 'id', 'fileId'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateDocumentsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('documents', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->unsignedInteger('documentId')->nullable(); | |
$table->string('name'); | |
$table->foreign('documentId') | |
->references('foreignId') | |
->on('file_locations'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('documents'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateFileLocationsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('file_locations', function (Blueprint $table) { | |
$table->increments('foreignId'); | |
$table->unsignedInteger('id')->nullable(); | |
$table->foreign('id') | |
->references('locationId') | |
->on('file_upload_locations'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('file_locations'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateFileUploadLocationsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('file_upload_locations', function (Blueprint $table) { | |
$table->increments('locationId'); | |
$table->unsignedInteger('fileId'); | |
$table->foreign('fileId') | |
->references('id') | |
->on('files'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('file_upload_locations'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateFilesTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('files', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('filename'); | |
$table->integer('size'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('files'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment