mercredi 12 janvier 2022

Laravel 5.8 - Creating a maintenance order and attaching to a car by foreign key

I hope I can ask this question correctly. I have a form to create a car and then another form to create a maintenance order that you can select a car by it's ID to create some maintenance for whichever car was picked. And it works fine in my database I get back the exact car_id selected and they look connected. However, in my browser it is saying that my cars have no relation, and I believe it has something to do with how I create the child in the controller? Am I overwriting the car_id with the request possibly? Here is some code for context.

BLADE:

@section('content')
    <div class="card-body border rounded">
        <form action="" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="form-group">    
                <div class="col-sm-8">
                    <label class="col-sm-4 col-form-label" id="useCar">Use Selected Car</label>
                    <select class="form-control" id="selectCar" name="car_selected" required focus>
                        <option value="" disabled selected>Please select car</option>        
                        @foreach($cars as $car)
                            <option value=""></option>
                        @endforeach
                    </select>
                </div>
            </div>
            <fieldset class="form-group">
                <div class="row">
                    <legend class="col-form-label col-sm-2 pt-0">Maintenance Work: </legend>
                        <div class="col-sm-10">
                            <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="oil_changes" id="gridChecks1">
                            <label class="form-check-label" for="gridChecks1">Oil Changes</label>
                            </div>
                            <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="tire_rotations" id="gridChecks2">
                            <label class="form-check-label" for="gridChecks2">Tire Rotations</label>
                            </div>
                            <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="tune_ups" id="gridChecks3">
                            <label class="form-check-label" for="gridChecks3">Tune-ups</label>
                            </div>
                            <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="repairs" id="gridChecks4">
                            <label class="form-check-label" for="gridChecks4">Repairs</label>  
                            </div>
                        </div>
                </div>
            </fieldset>
            <div class="form-group">
                <label for="maintenanceNotes">Maintenance Notes</label>
                <textarea class="form-control" name="notes" id="maintenanceNotes" rows="3"></textarea>
            </div>
            <br>
            <button class="btn btn-success">Maintenance Request</button>
        </form>
    </div>
@endsection

ROUTES:

Route::get('maintenance/create', 'MaintenanceController@create')->name('maintenance.create');
Route::post('maintenance/store', 'MaintenanceController@store')->name('maintenance.store');

CONTROLLER:

public function store()
    {
        $car_id = request('car_selected');
        $oil = strtoupper(request('oil_changes', 'off')) == 'ON';
        $tire = strtoupper(request('tire_rotations', 'off')) == 'ON';
        $tune = strtoupper(request('tune_ups', 'off')) == 'ON';
        $repair = strtoupper(request('repairs', 'off')) == 'ON';
        $note = request('notes');

        Maintenance::create([
            'car_id' => $car_id,
            'oil_changes' => $oil,
            'tire_rotations' => $tire,
            'tune_ups' => $tune,
            'repairs' => $repair,
            'notes' => $note
        ]);

        return redirect()->route('car.index');
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire