【问题标题】:Laravel - pass variable to functionLaravel - 将变量传递给函数
【发布时间】:2018-02-17 20:08:07
【问题描述】:

我正在为数据库创建查询:

        $test_2_lowest = TestsCorrect
        ::join('results', 'tests_correct.id', '=', 'results.test_result_id')
        ->where([
            ['results.user_id', '=', $user_id],
            ['results.test_id', '=', '2'], // <----
        ])->min('correct');

这里唯一可变的数字是'2'。 我想以某种方式将其作为变量传递,我不必声明多个变量即可从不同的测试中获得分数。

我尝试了输入名称为数字的表单:

<form action="{{route('results')}}" method="post">

<input type="hidden" name="test_id" value="3"/>

<button type="submit">Send</button></form>

和功能

    public function results(Request $test_id)
{
    $test_2_lowest = TestsCorrect
        ::join('results', 'tests_correct.id', '=', 'results.test_result_id')
        ->where([
            ['results.user_id', '=', $user_id],
            ['results.test_id', '=', $test_id->input('test_id');],
        ])->min('correct');
}

但它不起作用:(

【问题讨论】:

  • 从数组中删除;,这是一个语法错误。另请阅读minimal reproducible example解释您的“不起作用”。
  • 那个;实际上并没有在代码中,我把它粘贴在这里不好,“不起作用”的意思是“MethodNotAllowedHttpException”,原因是我有 method="post",应该是 method="put"

标签: php mysql laravel


【解决方案1】:

这里有一些简洁的语法

public function results(Request $request)
{
  $test_2_lowest = TestsCorrect::join('results', 'tests_correct.id', '=', 'results.test_result_id')
         ->where('results.user_id', '=', $user_id)
         ->where('results.test_id', '=', $request->get('test_id')),
  ])->min('correct');
}

【讨论】:

  • 感谢回复,但主要原因是method="post",应该是method="put"
猜你喜欢
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 2022-01-11
相关资源
最近更新 更多