Skip to content

Instantly share code, notes, and snippets.

@Helfull
Forked from DavidStrada/many_to_many.php
Last active June 28, 2016 19:09
Show Gist options
  • Save Helfull/2908484783e938e4488ccc617250fed2 to your computer and use it in GitHub Desktop.
Save Helfull/2908484783e938e4488ccc617250fed2 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