【问题标题】:Laravel Eloquent queries modificationLaravel Eloquent 查询修改
【发布时间】:2014-08-25 14:17:15
【问题描述】:

在我的应用程序中,我有三个表:问题、类别和主题。当我添加问题时,我选择了一个类别,它会添加类别名称和主题名称。

我试过这种方式;它有效,但我认为这不是最好的方法。

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',
        ));
    }
}

【问题讨论】:

  • 移除 $question->save(); if($question->save()) 应该做的工作。
  • 我在谈论顶部的 3 个查询!!
  • 为此,我要么使用为他们创建的 eloquent 模型,要么为他们创建 repos,
  • 我该如何使用它们!谢谢
  • 问题是什么?您是在问这样做是否可以?

标签: php mysql laravel eloquent


【解决方案1】:

就这么简单:

$category = DB::table('category')
 ->where('categoryId','LIKE',$category)
 ->first();

$categoryName = $category->name;
$themeId      = $category->themeId;
$themeName    = $category->themeName;

【讨论】:

    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 2015-02-22
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2015-03-11
    相关资源
    最近更新 更多