Skip to content

Instantly share code, notes, and snippets.

@adamgoose
Created August 6, 2014 21:19
Show Gist options
  • Save adamgoose/d517eca6e011d84f99cf to your computer and use it in GitHub Desktop.
Save adamgoose/d517eca6e011d84f99cf to your computer and use it in GitHub Desktop.
<?php
class Course extends Eloquent implements Collectable {
use SoftDeletingTrait;
use EventGenerator;
use TaggableTrait;
use ImagableTrait;
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function completedBy()
{
return $this->belongsToMany('User', 'course_progress')->withTimestamps()->withPivot(['scope_type', 'scope_id']);
}
/**
* @param Model $parent
* @param array $attributes
* @param string $table
* @param bool $exists
*
* @return CourseProgress|Pivot
*/
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
if($table == 'course_progress')
return new CourseProgress($parent, $attributes, $table, $exists);
return new Pivot($parent, $attributes, $table, $exists);
}
}
<?php
class CourseProgress extends Pivot {
use EventGenerator;
/**
* @var string
*/
protected $table = 'course_progress'; // The pivot table between users and courses
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('User');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function course()
{
return $this->belongsTo('Course');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function scope()
{
return $this->morphTo();
}
}
<?php
class User extends Eloquent implements UserInterface, RemindableInterface, CourseProgressScope {
use UserTrait, RemindableTrait, EventGenerator;
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function courseProgress()
{
return $this->belongsToMany('Course', 'course_progress')->withTimestamps()->withPivot(['scope_type', 'scope_id']);
}
/**
* @param Model $parent
* @param array $attributes
* @param string $table
* @param bool $exists
*
* @return CourseProgress|Pivot
*/
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
if($table == 'course_progress')
return new CourseProgress($parent, $attributes, $table, $exists);
return new Pivot($parent, $attributes, $table, $exists);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment