在收款和付款之间进行检查 [英] checking between seance and payment

查看:59
本文介绍了在收款和付款之间进行检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(08/09/2019)

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

I have a question concerning the payment verification.

例如,学生Leonard仅在09/09/2019 上接受培训.

For example, the student Leonard pays a training on 09/09/2019 only.

在我的表格Training中,我可以添加几个步骤(培训)????

In my form Training, I see that I can add several seances (trainings) ????

我可以阻止吗?

有关信息,在我的表Training中,我有5个外键(fk_former, fk_student, fk_motorbike, fk_typetraining, fk_payment).

For information, in my table Training, I have 5 foreign keys (fk_former, fk_student, fk_motorbike, fk_typetraining, fk_payment).

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

To join the table training and payment I do this:

模型培训

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


}

模型付款

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

我的关系还可以

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

I have to do how to limit the trainings? Because it's for now illimited... the number of seances is not taken into account with the payment.

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

public function store(Request $request)
    {
        $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_motorbike' => 'required',
                'fk_former' => 'required',
                'fk_student' => 'required'


        ]);


    $date_start = $request->get('date_seance'); 
    $hour_start = $request->get('hour_start'); 
    $hour_end = $request->get('hour_end'); 
    $fk_motorbike = $request->get('fk_motorbike');
    $fk_former = $request->get('fk_former');
    $fk_student = $request->get('fk_student');



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

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

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


    /*
    if(isset($trainings)){
        return redirect()->route('trainings.index')
        ->with('error', 'Duplicate ! ');
    }*/


    if(isset($johnsTrainings)){
        return redirect()->route('trainings.index')
        ->with('error', 'Limit ! ');
    }

        else{
            Training::create($request->all());
                return redirect()->route('trainings.index')
                    ->with('success', 'Add');
        }

    }

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

In testing your code I get this:

推荐答案

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

I think there are several things which need attention. You state:

在培训"表格中,我可以添加几个集会?

In my form Training, I see that I can add several seances ????

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

So I assume you want to link the ability to add trainings based on if a person pays for THAT training. And on your form, you see that someone can add more trainings, even though they haven't paid for them. And this is the problem.

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

I suggest you may wish to change your architecture on this and simplify your relationships. If you create a payment as a one-to-many relationship between Payment model and Training model, you don't have to worry about checking to see if the payment can apply to many dates.

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

So on your Training model the relationship is many:

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

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

And on your Payment model it has one Training only:

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

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

You'll have to tweak this code and maybe a tiny bit of the database (probably don't need fk_type_seance, but do need some kind of fk_training). But now it is very easy to check if you have a conflict for the payment of the training for this person by checking the payment against the specific Training.

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

if-check现在正在寻找付款方式是否适合该培训以及该学生-如果是,则它将停止由同一个人进行的同一培训的重复付款.

The if-check is now looking to see if the payment fits the training and the student - if so, it will stop a double payment for the same training by the same person.

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

For your list of trainings you'll need to come at this from the reverse. Pull the trainings with payments:

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

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

Then, in your list, if you are listing a specific person you will need to check if the payment has been made by that person in order to list that person's payment for this specific training. You'll have to work on this, but something like this in your controller:

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

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

EDIT: From here, check to see if the training from the form is in that collection of paid for trainings from this student (from the form):

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

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

You may also have success by just pulling John's payments and then linking to the Training models within each - you can loop on those as well. MIght be easier. There are several ways to do this, but this is getting a long way from you question - look for simplifying your architecture to make this work :)

这篇关于在收款和付款之间进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆