user11124425
user11124425

Reputation: 971

The dates and checking for a planning

I have a problem concerning the checking of my dates.

Example, my motorbike number 000001 is in revision from 30/08/2019 to 03/09/2019. The recording is good. (no problem)

However, if I add a revision on 02/09/2019 to 03/09/2019, I would like to add an error message explaining that the motorbike is already in revision.

enter image description here

My function index() is like this:

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

I think my problem is in my function store()???

Edit:

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



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

 $conflict2 = Revision::whereDate('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');
        }

enter image description here enter image description here

Upvotes: 1

Views: 60

Answers (2)

Watercayman
Watercayman

Reputation: 8178

I think you can use a similar test to see if the bike is already in revision, to the one that you have designed previously to test against bikes in revision for today.

There are a few ways to do this, but I will write the simplest one even though it may not be the most efficient.

I would take the start and end date from your input on the second bike (I don't know what this is, but I'm assuming you have someone trying to input a start and end date for a motorbike revision - let's call it newStart and newEnd for the input dates)

$newStart = $request->get('newStart');
$newEnd = $request->get('newEnd');

Then, run through both of these dates to see if either coincide with the current revision for this motorbike (you can do this in one query, but I'm expanding just to show what you are after):

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

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

If either of these is in revision, then send back the fail message:

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

This code is not exact, but I think this is all you need to determine if there is a conflict (you don't need the $exists stuff for determining a conflict, but you may need some code to store the new revision if there is NO conflict). You'll need to play with this a bit to get it working for you.

Upvotes: 2

Sher E Yr
Sher E Yr

Reputation: 106

You should have fields like start_date and end_date when submitting form.

use this for Revisions.

$revisions= Revisions::where('date_revision_start','>=',$request->start_date)
->orWhere('date_revision_end','<=',$request->end_date)
->first();

if($revisions){
 code...
}
else{
return error......
}

Upvotes: 1

Related Questions