两种形式的重复系统 [英] Duplicate system on two forms

查看:52
本文介绍了两种形式的重复系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是为摩托车创建小型驾驶学校.

My project is to create a small driving school for motorbike.

我有4张桌子:Former, Training, Revision, Motorbike

我的函数create()和store()适用于我的4种表单.

My functions create() and store() work for my 4 forms.

但是,我有一个大问题……如果,我的摩托车在revision上在03/09/201905/09/2019上,在摩托车000001

However, I have a big problem ... If, my motorbike is in revision on03/09/2019 to 05/09/2019 for the motorbike000001

,并且我以training的形式对03/09/2019上的预订进行了编码,并将其作为摩托车号码000001;通常情况下,摩托车不可用,我该如何处理?

and that I encode in my form training a booking on 03/09/2019 with as motorbike number000001; Normally the motorbike is unavailable, how can I handle that?

是否可以为形式revision-> training或反型training-> revision

Is it possible to create a duplicate system for the forms revision ->training or the inverse training ->revision

在我的控制器修订版中,我有这个:

In my Controller Revision I have this:

public function index()
    {
        $revisions = Revision::oldest()->paginate(5);
        return view('admin.revisions.index', compact('revisions'))
          ->with('i', (request()->input('page',1) -1)*5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()            
    {

        $motorbikes = Motorbike::all();
        return view('admin.revisions.create', compact('motorbikes','revisions'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $date_revision_start = $request->get('date_revision_start');
        $date_revision_end = $request->get('date_revision_end');
        $hour_start = $request->get('hour_start');
        $hour_end = $request->get('hour_end');
        $garage = $request->get('garage');
        $fk_motorbike = $request->get('fk_motorbike');

        $conflict = Revision::where('fk_motorbike', $request->get('fk_motorbike'))->whereDate('date_revision_start', "<=" , $date_revision_start)
            ->whereDate('date_revision_end', ">=", $date_revision_start)
            ->first();

        $conflict2 = Revision::where('fk_motorbike', $request->get('fk_motorbike'))->whereDate('date_revision_start', "<=" , $date_revision_end)->whereDate('date_revision_end', ">=", $date_revision_end)->first();



        if(isset($conflict2) || isset($conflict)){
            return redirect()->route('revisions.index')
             ->with('error', 'duplicate');
        }

       else{
        Revision::create($request->all());
            return redirect()->route('revisions.index')
                ->with('success', 'new data created successfully');
        }

    }

对于控制器培训

public function index()
    {
        $trainings = Training::oldest()->paginate(5);
        return view('admin.trainings.index', compact('trainings'));
                with('i', (request()->input('page', 1) -1) *5);
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   
        $motorbikes = Motorbike::all();
        $formers = Former::all();
        return view('admin.trainings.create', compact('motorbikes','formers','trainings'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_motorbike' => 'required',
                'fk_former' => 'required'


        ]);


       $exists = Training::where('date_seance', $request->get('date_seance'))->where('hour_start', $request->get('hour_start'))->where('hour_end', $request->get('hour_end'))->where('fk_motorbike', $request->get('fk_motorbike'))->count();

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

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

        } 


    }

您能帮我吗,因为我仍然是Laravel的初学者,所以我想解决这个问题.

Could you help me please, because I'm still a beginner in Laravel and I want to solve this problem.

预先感谢

修改

 $date_start = $request->get('date_seance'); 
       $fk_motorbike = $request->get('fk_motorbike');

       $hour_start = $request->get('hour_start');
       $hour_end = $request->get('hour_end');

        $conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
            ->whereDate('date_revision_start', "<=" , $date_start)
            ->whereDate('date_revision_end', ">=", $date_start)
            ->where('hour_start', "<=" , $hour_start)
            ->where('hour_end', "<=" , $hour_end)
            ->first();

        $conflictTraining = Training::where('fk_motorbike', $fk_motorbike)
            ->whereDate('date_seance', "=" , $date_start)
            ->where('hour_start', "<=" , $hour_start)
            ->where('hour_end', "<=" , $hour_end)
            ->first();

        if(isset($conflictTraining) || isset($conflictRevision)){
            return redirect()->route('trainings.index')
                 ->with('error', 'Duplicate ');
        }

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

推荐答案

这是可能的,并且您几乎已经有了所需的东西.您可以在重复的修订代码之后对培训重复的代码进行建模,并添加一个非常相似的修订重复代码以得到答案.

This is possible, and you have almost got what you need already. You can model the training duplicate code after the duplicate revision code and add a very similar revision duplicate code to get your answer.

我将详细解释并以代码方式重复我自己,但是希望这会更容易理解.只要对您有意义,就可以压缩此代码.并非最不重要的是,将重复的修订代码作为 Controller Revision 中的一种单独方法,以便同时 Controller Revision Controller Training >可以从中吸取教训.但这是稍后的-现在让我们开始吧.

I am going to explain in detail and repeat myself codewise, but hopefully this will be easier to understand. You can compress this code once it makes sense to you. Not the least of which would be to make the duplicate code for revision a separate method within your Controller Revision, so that both Controller Revision and Controller Training can draw from it. But that's later - for now let's get it working.

在您的控制器培训上,暂时删除$exists内容.首先,我们需要确保在用户希望对摩托车应用培训的同时没有任何修订.不过,在这种情况下,您的模型中只有一个训练日期,因此只有一个$conflict检查修订:

On your Controller Training, remove the $exists stuff for now. First we need to make sure there is no revision at the same time as the user wants to apply training for the motorbike. In this case, though, there is only one date for Training in your model, so only one $conflict check for the revision:

$date_start = $request->get('date_seance'); // I assume your form field for training is date_seance
$fk_motorbike = $request->get('fk_motorbike');

// NOTE: the $date_start MAY need to be converted to a Carbon instance if it doesn't work - same as before

$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
        ->whereDate('date_revision_start', "<=" , $date_start)
        ->whereDate('date_revision_end', ">=", $date_start)
        ->first();

现在,我们需要检查培训是否存在冲突.我们可以使用已经分配的相同PHP变量,只需更改我们要查询的模型即可.我认为每个模型的聚会时间为一天:

Now we need to check to see if there is a conflict for training. We can use the same PHP variables already assigned, just change the model we are querying. I assume seance is ONE day per your model:

$conflictTraining = Training::where('fk_motorbike', $fk_motorbike)
        ->whereDate('date_seance', "=" , $date_start)
        ->first();

然后,这与我们在版本检查中使用的if-check非常相似:

And then it is a very similar if-check to the one we used in the revision check:

if(isset($conflictTraining) || isset($conflictRevision)){
     return redirect()->route('trainings.index')
            ->with('error', 'Duplicate ');
else{
     Training::create($request->all());
        return redirect()->route('trainings.index')
            ->with('success', 'Add');
}

这篇关于两种形式的重复系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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