在我的应用程序中,我有三个表:问题,类别和主题。当我添加一个问题时,我选择一个类别,它会添加类别名称和主题名称。
我试过这个方法;这方法有效,但我不认为这是最好的方法。
public function store()
{
$category = Input::get('category');
$categoryName = DB::table('category')
->where('categoryId', 'LIKE', $category)
->pluck('categoryName');
$themeId = DB::table('category')
->where('categoryId', 'LIKE', $category)
->pluck('themeId');
$themeName = DB::table('theme')
->where('themeId', 'LIKE', $themeId)
->pluck('themeName');
$question = new questionList;
$question->questionId = Input::get('questionId');
$question->question = Input::get('question');
$question->rightAnswer = Input::get('rightAnswer');
$question->explanation = Input::get('explanation');
$question->wrongAnswer1 = Input::get('wrongAnswer1');
$question->wrongAnswer2 = Input::get('wrongAnswer2');
$question->wrongAnswer3 = Input::get('wrongAnswer3');
$question->theme = $themeName;
$question->category = $categoryName;
$question->difficulty = Input::get('difficulty');
$question->save();
if ($question->save())
{
return Response::json(array(
'status' => 'ok',
'message' => 'success',
));
}
}
就这么简单:
$category = DB::table('category')
->where('categoryId','LIKE',$category)
->first();
$categoryName = $category->name;
$themeId = $category->themeId;
$themeName = $category->themeName;