【发布时间】:2014-04-08 11:14:48
【问题描述】:
假设我有以下表格:
User:
-userID
-userName
...
Exercises:
-exerciseID
...
User模特:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $primaryKey = 'userID';
...
public function hasPassedExercises() {
return $this->hasMany('Exercise', 'exerciseID');
}
}
我想说一个User有很多completedExercises,所以当用户完成一个练习时,我像这样更新模型:
Route::post('dbm/userPassedExercise', function () {
$user = User::with('hasPassedExercises')->find($_POST['userID']);
$exercise = Exercise::find($_POST['exerciseID']);
$user->hasPassedExercises->save($exercise);
});
但是,据我所知,这对任何基础表都没有影响。我正在尝试理解文档并查看它如何应用于我的问题。所以我的问题是在这里做什么是正确的。
我是否应该创建一个表users_completed_exercises,其中有userID 和exerciseID 作为外键,如果是这样,当我进行更新时如何将它们链接到我的用户?还是有更优雅的解决方案?
【问题讨论】:
标签: php database laravel eloquent