检查收款和付款

时间:2019-09-07 17:27:11

标签: laravel laravel-5

编辑:(08/09/2019)

我有一个有关付款验证的问题。

例如,学生const x = {name:null}; const y = {name:false}; const z = {name:true}; console.log(!x.name && x.name !== null); // false console.log(!y.name && y.name !== null); // true console.log(!z.name && z.name !== null); // false 仅在Leonard上接受培训{strong> 。

enter image description here

在我的09/09/2019表格中,我可以添加几个会议(培训)????

enter image description here

我可以阻止吗?

有关信息,我在表Training中有5个外键Training

enter image description here

要参加餐桌培训和付款,我需要这样做:

模型培训

(fk_former, fk_student, fk_motorbike, fk_typetraining, fk_payment)

模型付款

class Training extends Model
{
    //
    protected $fillable = ['date_seance', 'hour_start', 'hour_end', 'fk_motorbike', 'fk_former', 'fk_student'];
    protected $dates = ['date_seance'];

    public function motorbikes(){
        return $this->belongsTo('App\Motorbike', 'fk_motorbike');
    }

    public function formers(){
        return $this->belongsTo('App\Former', 'fk_former');
    }

    public function students(){
        return $this->belongsTo('App\Student', 'fk_student');
    }

    public function payments(){
      return $this->hasMany('App\Payment', 'fk_training');
  }


}

我的关系还可以

enter image description here

enter image description here

我必须怎么做才能限制培训?因为现在是无限的...付款时不考虑会晤的次数。

这是我的控制器培训上的函数store()编辑

class Payment extends Model
{

    protected  $fillable = ['date_payment', 'fk_student', 'method_payment',  'fk_type_seance', 'number_seance',  'total', 'fk_training']; 
    protected $dates = ['date_payment'];


    public function students(){

        return $this->belongsTo('App\Student', 'fk_student');
    }


    public function trainings(){
        return $this->belongsTo('App\Training', 'fk_training', 'id');
    } 

在测试您的代码时,我得到了:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为有几件事需要注意。您声明:

  

在“培训”表格中,我看到可以添加几个集会?

因此,我假设您想链接某人是否为THAT培训付费的基础上添加培训的功能。在您的表格上,您可以看到有人可以添加更多培训,即使他们没有为此付费。这就是问题所在。

我建议您不妨在此基础上更改架构,并简化您的关系。如果您将付款创建为付款模型和培训模型之间的一对多关系,则不必担心检查付款是否可以应用许多日期。

因此在您的培训模型中,这种关系很多:

public function payments(){
    return $this->hasMany("App\Payment", 'your_foreign_key', 'your_local_key');
}

在您的付款模型上,该培训仅进行一次培训:

public function training(){
   return $this->belongsTo("App\Training");
} 

您必须调整此代码,也许还要调整数据库的一小部分(可能不需要fk_type_seance,但确实需要某种fk_training)。但是现在,通过针对特定的培训检查付款,很容易检查您是否为此人支付培训费用有冲突。

$PaymentHasBeenMadeForThisTraining = Payment::where('fk_student', $fk_student)  
    ->where('fk_training', $request->get('fk_training')) 
    ->first();  

if-check现在正在查看付款是否适合该培训和该学生—如果是,则它将停止同一个人对同一培训的两次付款。

对于您的培训列表,您需要从相反的角度进行。付费进行培训:

 $trainings = Training::with('payments')->get();

然后,在您的列表中,如果您要列出特定人员,则需要检查该人员是否付款,以便列出该人员针对该特定培训的付款。您必须对此进行操作,但是在控制器中需要执行以下操作:

 $thisStudentsTrainings = Training::with(['payments' => function($query) use($fk_student){
        $query->where('fk_student', $fk_student);
    }])->get();

编辑: 在此处,检查表格中的培训是否在该学生的付费培训集合中(表格中):

$hasPaidForThisTraining = $thisStudentsTrainings ->contains('id', $request->get('fk_training'));

您也可能会成功,只需提取John的付款,然后链接到每个模型中的 Training 即可-您也可以循环使用这些模型。轻松一点。有几种方法可以做到这一点,但这对您的问题还有很长的路要走-寻找简化您的体系结构以使这项工作:)

相关问题