user11124425
user11124425

Reputation: 971

Change the status automatically after the add of a revision

I have 2 forms, the first is the form motorbikes with 3 fields (matriculation, number_motorbike, status).

enter image description here

Then, we have the form revisions with 4 fields (date_revision_start, date_revision_end, garage, fk_motorbike)

enter image description here

Here in the form motorbikes the status of the number motorbike must to be automatically unavailable.

enter image description here

I must to work on which controller to automate the availability status of the motorbike?

Edit: 09 / 07 / 2018

Controller Motorbike

public function store(Request $request)
    {      

       $bikeIdsDown = Revision::where('date_revision_start', "<", Carbon::now())->where('date_revision_end', ">", Carbon::now())->pluck('id')->toArray();

       return view('motorbikes.index', compact('motorbikes', 'bikeIdsDown'));

        }

Index.blade.php

@foreach($motorbikes as $bike)
          <tr>
            <td>{{ $bike->martriculation }}</td>
            <td>{{ $bike->number_motorbike }}</td>
            <td>
             @if(in_array($bike->id, $bikeIdsDown))
              UNAVAILABLE
              @else
              Available
            @endif</td>
            <td>
                          <form action="{{ route('motorbikes.destroy', $motorbike->id) }}" method="post">

                            {{ csrf_field() }}
                            {{ method_field('DELETE') }}
                            <a href="" class="btn btn-primary">Details</a>
                            <a href="{{ url('motorbike/' .$motorbike->id. '/edit') }}" class="btn btn-warning">Editer</a>
                @csrf
                @method('DELETE')

                            <button type="submit" class="btn btn-danger">Supprimer</button>
                        </form>

                    </td>
                </tr>
                @endforeach

enter image description here

Upvotes: 0

Views: 115

Answers (1)

Watercayman
Watercayman

Reputation: 8178

To show the list of motorbikes that are unavailable, you would need to work on your Motorbike Controller. Specifically, the edit() method if you are making changes to the status manually, the create() method if you are making a new motorbike, or most likely, the show() method to just display that table you have above where it shows if they are available or not.

Now... to get the part where it displays if the bike is unavailable... I assume it is unavailable when it is down for repair (revision). Since I don't know what your relations are, we can figure this out using a simple pull from all revisions to see which bikes should be down currently. So, in one of those methods I noted above (or all of them), first let's see which bikes are down for revision, during the current time:

$bikeIdsDown = Revision::where('date_revision_start', "<" Carbon::now())->where('date_revision_end', ">", Carbon::now())->pluck('id')->toArray();

Not the most efficient, but simplest to follow hopefully.

Now that we have these unavailable motorbike ids, if we compact them and send them through to the blade page, we can determine whether to write 'available or 'unavailable' in the motorbike field. So, as you are looping through the motorbikes on the blade page, maybe something like this:

@foreach($motorbikes as $bike) 
    {{$bike->martriculation }}  // <-- these might all be in <td> or something
    {{$bike->number}}
    @if(in_array($bike->id, $bikeIdsDown))
        UNAVAILABLE
    @else
        Available
    @endif
@endforeach

Hopefully this will give you the idea.

Upvotes: 1

Related Questions