总共有2个模型

make a total with 2 models

我有一个名为 seances 的 table,有 3 个字段(id、type_seance、价格)

然后,我还有一个名为payments的table,有4个字段(id,fk_type_seance,number_seance,总计)

什么时候,我在我的表格里加了一条录音Payment

如果,我 select 降神会的类型 Theoretical(价格是 100 美元)并且我输入了 2 次降神会,总金额是 200 美元。

我的 Controller Payment 有问题,如何调整我的字段价格和 fk_type_seance ??检索总数?

$data['total'] = $request->price->fk_type_seance * $request->number_seance;

这是我的代码:

public function store(Request $request)
    {      

        $request->validate([
           'fk_type_seance' => 'required',
           'number_seance' => 'required',
           'total' => 'required'

         ]);

        $exists = Payment
                     ::where('fk_type_seance', $request->get('fk_type_seance'))
                     ->where('number_seance', $request->get('number_seance'))
                     ->where('total', $request->get('total'))
                     ->count();

       if (!$exists)
       {
            $data = $request->all(); 

            $data['total'] = $request->price->fk_type_seance * $request->number_seance;
            //Payment::create($request->all());
            Payment::create($data);
            return redirect()->route('payments.index')
                ->with('success', 'new data created successfully');
        }
        else
        {
            return redirect()->route('payments.index')
                ->with('error', 'doublon');

        }   

    }

现在,这是我的付款表格,我对总数有疑问。

编辑 代码 jtwes

public function store(Request $request)
    {      

        $request->validate([
           'fk_type_seance' => 'required',
           'number_seance' => 'required',
           'total' => 'required'

         ]);

        $exists = Payment::where('fk_type_seance', $request->get('fk_type_seance'))->where('number_seance', $request->get('number_seance'))->where('total', $request->get('total'))->count();

       if (!$exists){
            $seance = Seance
                  ::where('id','=',$request->get('fk_type_seance'))
                  ->first();
            $seance = $request->all(); 
            if ($seance) $total = $seance->price * $request->number_seance;
            Payment::create($seance);
            return redirect()->route('payments.index')
                ->with('success', 'new data created successfully');
        }

        else{
            return redirect()->route('payments.index')
                ->with('error', 'doublon');

        }   

    }

你必须在计算之前得到你的降神... 像这样:

$seance = Seance::where('id','=',$request->get('fk_type_seance))->first();
if ($seance) $total = $seance->price * $request->number_seance;

这里是问题开始的地方:

$data['total'] = $request->price->fk_type_seance * $request->number_seance;

此处您根据 $request->price->fk_type_seance$request->number_seamce 的乘积计算 total,但在您的请求验证中没有 price 变量。 . 即使是这样,将 id (fk_type_seance) 乘以一个数量也没有任何意义。你应该乘以 price 和数量。

因此,将其替换为:

// first find your seance instance:
$seance = Seance::find($request->fk_type_seance);
// then calculate the total
$data['total'] = $seance->price * $request->number_seance;

然后你得到正确的 total 金额。

您还应该对请求负载进行额外的验证。在这里,我添加了一个 exists 验证来检查 fk_type_seanceseances table 中是否有匹配的 id,但首先确保它是一个 integer .另外,你如何收到总数,你在视图中计算它?无论如何,它应该是这样的:

$request->validate([
   'fk_type_seance' => 'required|integer|exists:seances,id',
   'number_seance'  => 'required|integer',
   'total'          => 'required|numeric',
 ]);

所以你现在的函数就像下面这样。请注意,我使用 $request->only(...) method 而不是 ->all() 来仅获取所需的数据(出于安全原因):

public function store(Request $request)
{      
    $request->validate([
       'fk_type_seance' => 'required|integer|exists:seances,id',
       'number_seance'  => 'required|integer',
       'total'          => 'required|numeric',
     ]);

    $data = $request->only(['fk_type_seance', 'number_seance', 'total']);

    $exists = Payment
          ::where('fk_type_seance', $data['fk_type_seance'])
          ->where('number_seance', $data['number_seance'])
          ->where('total', $data['total'])
          ->count();

   if ( ! $exists)
   {
        $seance = Seance::find($data['fk_type_seance']);
        $data['total'] = $seance->price * $data['number_seance'];
        Payment::create($data);

        return redirect()
           ->route('payments.index')
           ->with('success', 'new data created successfully');
    }
    else
    {
        return redirect()
            ->route('payments.index')
            ->with('error', 'doublon');
    }   

}