Extract and save data from several related tables in Laravel

1

I am developing an enrollment form in Laravel 5.4 , as I have created five tables that have a one-to-many relationship:

The main table tblFicha has a one to many relationship with the tables: tblDiocesis , tblRegionales - > this table is the parent of 'tblDistrito' and finally 'tblFuncion' has automatic values, except the table Fichas , which is where the user will create their inscriptions. From Laravel .

I have done all the relationships correctly, my question is: How do I develop the code in the Store to save the values?

Form:

The code of the form:

<div class="form-group">{!!Form::text(
    'nombre', null, ['class'=>'form-control', 'placeholder'=>'Escribe tu  nombre']
)!!}</div>
<div class="form-group">{!!Form::text(
    'apellido', null, ['class'=>'form-control', 'placeholder'=>'Escribe tu apellido']
)!!}</div>
<div class="form-group">{!!Form::text(
    'email', null, ['class'=>'form-control', 'placeholder'=>'Escribe tu  email']
)!!}</div>
<div class="form-group">{!!Form::text(
    'cedula', null, ['class'=>'form-control', 'placeholder'=>'Escribe tu cedula']
)!!}</div>
<div class="form-group">{!!Form::text(
'telefono', null, ['class'=>'form-control', 'placeholder'=>'Escribe tu telefono']
)!!}</div>
<div class="form-group">{!!Form::text( 'congregaciones', null,
    ['class'=>'form-control', 'placeholder'=>'Si perteneces a una congregaciones']
)!!}</div>
<div class="form-group">
    <!--Diocesis -->
    {!! Form::select('diocesi_id',['placeholder'=>'Selecciona tu diocesis'])!!}</div>
<div class="form-group">
    <!--Regionales-->
    {!! Form::select(
        'regional_id',['placeholder' =>'Selecciona tu Regional Educativo']
    )!!}</div>
<div class="form-group">
    <!--Distritos-->
    {!! Form::select('distritos',['placeholder' => 'Si seleccionas tu distrito']
    )!!}</div>
<div class="form-group">
    <!--Funciones-->
    {!! Form::select('funcion_id', ['placeholder' => 'Selecciona tu funcion'])!!}</div>
<div class="form-group ">{!!Form::submit(
    'Guardar nueva inscripcion', ['class'=>'btn btn-primary']
)!!}<a href="ficha" class="btn btn-danger">Cancelar</a></div>

The tblFicha relationships with the others:

class Ficha extends Model
{
    //
    protected $fillable = [
        'nombre', 'apellido','cedula','email','telefono','diocesi_id',
        'congregaciones', 'regional_id', 'funcion_id'
    ];
    public function regionales(){return $this->belongsTo('App\Regional');}
    public function cargos(){return $this->belongsTo('App\Cargo');}
    public function diocesis(){return $this->belongsTo('App\Diocesi');}
}

The controller (is where it gets complicated):

public function store(Request $request)
{
    //
    $ficha = new Ficha;

    $ficha->nombre = $request->nombre;
    $ficha->apellido = $request->apellido;
    $ficha->cedula = $request->cedula;
    $ficha->email = $request->email;
    $ficha->telefono = $request->telefono;
    $ficha->diocesi_id = $request->diocesi_id;
    $ficha->congregaciones = $request->congregaciones;
    $ficha->regional_id = $request->regional_id;
    $ficha->funcion_id = $request->funcion_id;

    $ficha->save();

    return redirect('ficha')->with('msg', 'Se inscribio correctamente, gracias, te esperamos.');
}
    
asked by Gali 12.03.2017 в 22:49
source

1 answer

1

Hello, you can instantiate each model and add them

$ficha = new Ficha;

$ficha->nombre = $request->nombre;
$ficha->apellido = $request->apellido;
...
...
$regional = new Regional();
$regional->name = 'xxx'
$ficha->regionales($regional);
...
$ficha->save();

Or also adding them directly

$ficha->regionales(['name'=>'xxx']);
$ficha->save();
    
answered by 14.03.2017 в 15:43