Create query in several tables with Laravel 5.6

0

I want to prevent the user from deleting a record if it is already in use, for example: Patient-id_patient table have interaction with table Accidents or Table Appointments Clear by the primary key id_patient.

The idea is that if it already has records it is not deleted, but if it does not have to be deleted by the user.  I am trying to do it by means of disabling the delete button, but I can not find how to arm the condition so that it works for me as it should.

My code is this:

<td>
        <a class="btn btn-primary" href="{{ route('pacientes.edit',$paciente->id_paciente) }}" title="Editar"><span class="fas fa-edit"></span></a>
            {!! Form::open(['method' => 'DELETE','route' => ['pacientes.destroy', $paciente->id_paciente],'style'=>'display:inline','onsubmit' => 'return ConfirmDeleteModel("el paciente","'.$paciente->nombre_pac.'","'.$paciente->id_paciente.'")']) !!}
            @if ($paciente->hacer_condicion)
            <button class='btn btn-danger' type='submit' value='submit' title="Borrar" disabled='false'>      <span class='fas fa-times'> </span>
                </button>
                @else
                <button class='btn btn-danger' type='submit' value='submit' title="Borrar" >      <span class='fas fa-times'> </span>
                    @endif

            {!! Form::close() !!}

    </td>

It works to delete a record without more interactions but when wanting to eliminate a patient who has an accident or appointment I can not disable the button

I hope and you can guide me I'm relatively a Rookie in Laravel I still do not understand it well

    
asked by Alexander Alvear 11.10.2018 в 00:32
source

1 answer

0

You can do it in two ways: by means of external keys in database that link the tables and prevent the deletion of already linked records or, by code, checking before deleting the record. I also tell you that the two ways can coexist at the same time, they are not mutually exclusive.

Foreign keys

For the first case, it is enough to define them without putting anything as an action in the delete. For example, if we have two tables: pacientes and citas , assuming they are joined by the field id_paciente , the foreign key in MySQL would be defined as follows:

ALTER TABLE pacientes ADD CONSTRAINT fk_citas_pacientes FOREIGN KEY (id_paciente) REFERENCES citas (id_paciente);

You should put some code to deal with this exception by removing the patient in the controller to show a friendlier message to the user

By code

In this case you have many options but I will detail one:

  • We relate the Patient and Appointment models using hasMany and belongsTo:
  •   

    Patient Model

    public function citas()
    {
        return $this->hasMany('App\Cita');
    }
    
      

    Appointment Model

    public function paciente()
    {
        return $this->belongsTo('App\Paciente');
    }
    
  • In the controller, in the method that removes a patient, we check if you have appointments:
  •   

    Patient Controller Controller

    if ($paciente->citas()->count() != 0) {
        // No dejamos eliminar y nos salimos
    }
    
  • In the view, we do not show the delete button if the patient has appointments:
  •   

    Patient View

    @if ($paciente->citas()->count() == 0) 
        <button class='btn btn-danger' type='submit' value='submit' title="Borrar" disabled='false'><span class='fas fa-times'></span></button>
    @endif
    
        
    answered by 11.10.2018 / 12:20
    source