Skip to content

Instantly share code, notes, and snippets.

@DavidStrada
Forked from Helfull/many_to_many.php
Created June 28, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidStrada/c887085425ff78f6be50e8a30083a278 to your computer and use it in GitHub Desktop.
Save DavidStrada/c887085425ff78f6be50e8a30083a278 to your computer and use it in GitHub Desktop.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = ['name', 'content'];
public function laws()
{
return $this->belongsTo(Law::class);
}
public function references()
{
return $this->belongsToMany(Article::class, 'article_reference', 'article_id', 'reference_id');
}
public function referenced()
{
return $this->belongsToMany(Article::class, 'article_reference', 'reference_id', 'article_id');
}
}
<?php
Schema::create('article_reference', function(BluePrint $table){
$table->unsignedInteger('article_id');
$table->unsignedInteger('reference_id');
$table->foreign('article_id')->references('id')->on('articles');
$table->foreign('reference_id')->references('id')->on('articles');
$table->primary(['article_id', 'reference_id']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment