0

I created an edit form to update values in my database and then show it on the main page. The problem is that it doesn't save the data to DB.

the request passes: Status Code: 302 Found with all the values that I want to change (for example adding phone and mail): effective_date_practitioner: 2019-01-01 expiry_date_practitioner: phone_number_practitioner: 918273645 mobile_number_practitioner: email_practitioner: test@test.pl practitioner_specialty_id_update: 1 effective_date_specialty: 2019-01-01 expiry_date_specialty:

Edit blade

<div class="edit-practitioner" style="display: none;">
    <form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
          class="j-pro" id="update-practitioner-form">
        @csrf
        <div class="j-content">
            <div id="j-row-id" class="j-row">
                <div class="row">
                    <div class="col-sm-12 col-lg-12 col-xl-5">
                        <div class=" j-unit">
                            <div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
                                <span>Practitioner Information</span>
                            </div>
                            <label class="j-label">{{trans('personalData.effectiveDate')}}</label>
                            <div class="j-input">
                                <input type="date" value="{{$practitioner->effective_date}}"
                                       name="effective_date_practitioner">
                            </div>

                            <label class="j-label">{{trans('personalData.expiryDate')}}</label>
                            <div class="j-input">
                                <input type="date" value="{{$practitioner->expiry_date}}"
                                       name="expiry_date_practitioner">
                            </div>

                            <label class="j-label">{{trans('personalData.phoneNumber')}}</label>
                            <div class="j-input">
                                </label>
                                <input type="tel" value="{{$practitioner->phone}}"
                                       name="phone_number_practitioner">
                            </div>

                            <label class="j-label">{{trans('personalData.mobileNumber')}}</label>
                            <div class="j-input">
                                <input type="tel" value="{{$practitioner->mobile}}"
                                       name="mobile_number_practitioner">
                            </div>

                            <label class="j-label">{{trans('personalData.email')}}</label>
                            <div class="j-input">
                                <input type="email" value="{{$practitioner->email}}"
                                       name="email_practitioner">
                            </div>
                        </div>
                    </div>
                    <div class="col-xl-1 j-unit"></div>
                    <div class="col-sm-12 col-lg-12 col-xl-6">
                        <div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
                            <span>{{trans('settings.specialty')}}</span>
                        </div>
                        <select name="practitioner_specialty_id_update"
                                id="practitioner_specialty_id_update"
                                class="form-control-practitioner required">
                            @foreach($specialties as $specialty)
                                <option
                                    value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
                            @endforeach
                        </select>
                        <label class="j-label">{{trans('personalData.effectiveDate')}}</label>
                        <div class="j-input">
                            <input type="date" value="{{$practitioner_specialty->effective_date}}"
                                   name="effective_date_specialty">
                        </div>

                        <label class="j-label">{{trans('personalData.expiryDate')}}</label>

                        <div class="j-input">
                            <input type="date" value="{{$practitioner_specialty->expiry_date}}"
                                   name="expiry_date_specialty">
                        </div>
                    </div>
                </div>
            </div>
            <div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
            <button type="submit"
                    class="btn btn-editpanel btn-success btn-round">Save changes
            </button>
            <!-- end /.footer -->
            <button id="update-cancel-button-practitioner" type="button"
                    class="btn btn-editpanel btn-danger btn-round">Cancel
            </button>
        </div>
    </form>
</div>

web.php

Route::post('/practitioner/update/{id}', 'Crud\Settings\PractitionerController@updatePractitioner')->name('updatePractitioner');

Controller:

<?php

namespace App\Http\Controllers\CRUD\Settings;

use App\Models\Practitioner;
use App\Models\PractitionerCompetenceLevel;
use App\Models\PractitionerSpecialty;
use App\Repositories\PractitionerRepository;
use Illuminate\Http\Request;

class PractitionerController extends CrudController
{

    protected $repository;

    public function __construct(PractitionerRepository $repository)
    {
        $this->middleware('auth');
        $this->repository = $repository;
    }

    public function updatePractitioner($id, Request $request)
    {
        $this->validate($request, [
                'effective_date_practitioner' => 'required',
                'effective_date_specialty' => 'required',
            ]
        );
        $input = $request->all();
        $input['data'][$this->repository->getIdName()] = $id;
        $this->repository->update($input['data']);
        return back()->with('successUpdate', 'Practitioner has been updated!');
    }
}

My guess is that the data I want to update belongs to two different tables in DB one called practitioner and the other one called practitioner_specialty

S0ul3r
  • 153
  • 1
  • 9
  • We need to see the full code of the controller. Also, you have an error in updatePractitioner's method argument definition. It should start with Request $request and then all of the route params. – Andrii H. Jun 25 '19 at 14:08
  • _"My guess is .."_ If this is your code, why are you guessing? – elbrant Jun 25 '19 at 14:10
  • 1
    Take a look at [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – t00n Jun 25 '19 at 14:14

1 Answers1

0

As per your code you are trying to do mass assignment to your model

You may do this using the $fillable property on the model

protected $fillable = ['effective_date_practitioner','effective_date_specialty',......];

And you can use attach() method to updated data in related tables

bhavinG
  • 19
  • 3