总共有2个模型

时间:2019-08-12 21:25:11

标签: php laravel laravel-5 eloquent laravel-5.8

我有一个名为seances的表,其中包含3个字段(id,type_seance,price)

enter image description here

然后,我还有一个名为payments的表,其中包含4个字段(id,fk_type_seance,number_seance,total)

何时,我以Payment格式添加录音

enter image description here

enter image description here

如果,我选择集会Theoretical的类型(价格为100美元),并且输入2个类似集会的数量,则总计为200美元。

我的控制者付款有问题,如何调整我的现场价格和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');

        }   

    }

现在,这是我的付款表格,我对总额有疑问。 enter image description here

编辑代码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');

        }   

    }

2 个答案:

答案 0 :(得分:1)

在计算之前,您必须先保持冷静... 像这样:

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

答案 1 :(得分:1)

问题出在这里:

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

在这里,您基于total$request->price->fk_type_seance的乘积来计算$request->number_seamce,但是在您的请求验证中,您没有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_seance表中seances是否具有匹配的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');
    }   

}