Laravel + VueJS 完成系统

Completion system with Laravel + VueJS

我已经尝试了好几个小时来为完成系统定义我的关系,但我失败了。

我有一个 table 个用户和一个 table 个剧集,如果用户完成了一个剧集,我想加入我的观点。

我用 user_id 和 episode_id 创建了一个“完成”table 以及一个名为“已完成”的布尔字段

是多对多关系吗?我想要类似 $episode->completed 的东西,如果登录用户完成课程,它会给我 True ,但我找不到我的路......我只想知道如何定义我的关系,而不是整个工作完成。

非常感谢!!!!

我相信您可以告诉 Laravel 使用什么 table 并查询数据透视表列。

//user model
public function episodes(){
    return $this->belongsToMany( 'App\Episode', 'completions', 'user_id', 'episode_id' );
}
public function completedEpisodes(){
    return $this->belongsToMany( 'App\Episode', 'completions', 'user_id', 'episode_id' )
    ->wherePivot('completed','=', true)->get();
}

//episode model
public function users(){
    return $this->belongsToMany( 'App\User', 'completions', 'episode_id', 'user_id' );
}

另一种方法是将你的枢轴创建为 episode_user,laravel 将自动将其检测为枢轴,向该 table 添加一个完整的布尔值,它只需:

//user model
public function episodes(){
    return $this->belongsToMany('App\Episode');
}
public function completedEpisodes(){
    return $this->belongsToMany('App\Episode')
    ->wherePivot('completed','=', true)->get();
}

//episode model
public function users(){
    return $this->belongsToMany('App\User');
}

查询剧集是否完整:

//可能吧,没试过这个

//user
    public function hasCompletedEpisode($id){
        return $this->belongsToMany('App\Episode')->wherePivot('episode_id','=', $id)
        ->wherePivot('completed','=', true)->get();
}

//episode
    public function hasCompletedEpisode($id){
    $user_id = Auth::id();
    return $this->belongsToMany('App\User')->wherePivot('user_id','=', $user_id)
    ->wherePivot('completed', true)->get();
}

如果我是你,我会使用 custom intermediate table。您可以按如下方式实现:

迁移

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('episodes', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('watches', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('epsiode_id');
    $table->boolean('completed');
    $table->timestamps();
});

型号

class User extends Model
{
    protected $guarded = [];
    
    public function watchedEpisodes()
    {
        return $this->hasMany(Episode::class)->using(Watch::class);
    }
    
    public function watches()
    {
        return $this->hasMany(Watch::class);
    }
}

class Episode extends Model
{
    protected $guarded = [];
    
    public function users()
    {
        return $this->hasMany(User::class)->using(Watch::class);
    }
}

class Watch extends \Illuminate\Database\Eloquent\Relations\Pivot
{
    protected $table = 'watches';
    protected $guarded = [];
    
    public static function create(User $user, Episode $episode, bool $completed = false)
    {
        $watch = new self();
        $watch->user_id = $user->id;
        $watch->epsiode_id = $episode->id;
        $watch->completed = $completed;
        $watch->save();
        
        return $watch;
    }
    
    public function user()
    {
        return $this->hasOne(User::class);
    }
    
    public function episode()
    {
        return $this->hasOne(User::class);
    }
}

示例使用

$user = User::create(['name' => 'Big Watcher']);

$episode1 = Episode::create(['name' => 'Episode 1']);
$episode2 = Episode::create(['name' => 'Episode 2']);
$episode3 = Episode::create(['name' => 'Episode 3']);
$episode4 = Episode::create(['name' => 'Episode 4']);

Watch::create($user, $episode1);
Watch::create($user, $episode2);
Watch::create($user, $episode3);

return $user->watchedEpisodes;