Laravel + VueJS 的补全系统

Posted

技术标签:

【中文标题】Laravel + VueJS 的补全系统【英文标题】:Completion system with Laravel + VueJS 【发布时间】:2020-12-02 18:52:09 【问题描述】:

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

我有一个表格 Users 和一个表格 Episodes,如果用户完成了一个情节,我想进入我的视图。

我创建了一个包含 user_id 和 episode_id 的“completions”表以及一个名为“completed”的布尔字段

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

非常感谢!!!!

【问题讨论】:

一般来说,对于一集,用户必须有一个完成标志。而且,我认为您需要将其链接到诸如 user->episode->completed 之类的逻辑,而不是 $episode->completed。完成表可以是连接用户和剧集的枢纽。所以用户和情节之间的关系应该是多对多的。 【参考方案1】:

我相信你可以告诉 Laravel 使用什么表,也可以查询数据透视列。

//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 会自动将其检测为枢轴,向该表添加一个完整的布尔值,它只需:

//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();

【讨论】:

我终于成功了 :) 我决定从用户那里获取剧集并确定该剧集是否已在 JS 函数中观看。谢谢!【参考方案2】:

如果我是你,我会使用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;

【讨论】:

以上是关于Laravel + VueJS 的补全系统的主要内容,如果未能解决你的问题,请参考以下文章

001_Spring之xml的class的补全(eclipse)

Bash 补全详解

Laravel 路由问题:自动重定向到根文件夹

eclipse怎么去掉变量自动补全?

使用 laravel 的 docx 文件下载系统 - vuejs

linux环境下Python的自动补全