Skip to content

Instantly share code, notes, and snippets.

@burlresearch
Last active December 9, 2017 04:03
Show Gist options
  • Save burlresearch/0ccfcb79697db4388b960f5fd66ce950 to your computer and use it in GitHub Desktop.
Save burlresearch/0ccfcb79697db4388b960f5fd66ce950 to your computer and use it in GitHub Desktop.
Laravel relationships with multiple linking tables.
<?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');
}
}
<?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');
}
}
<?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');
}
}
<?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');
}
}
<?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');
}
}
<?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');
}
}
<?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');
}
}
<?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