Laravel 5.5 and Request

0

Good morning, I will try to explain myself as best as possible, I am new to Laravel

What I want to do is be able to save DB fields with data but they are not in the form, because I bring that data from a webservice, which works well, check it out.

I understand that the Request is executed before the STORE, and I assume that there is the problem. Whether I remove these fields from the Request or leave them the same, the error is thrown at me. The only way I found it is to save 1 by 1 the fields and even those that are in the form.

For example

public function store(DRequest $dRequest, D $d)
{
    try {
        $respuesta = $this->search($dRequest->input('nro_afiliado'), $dRequest->input('fecha'));
        $dRequest->apellido_nombre = $respuesta['apellido_nombre'];
        $dRequest->sexo = $respuesta['sexo'];
        $dRequest->edad = $respuesta['edad'];
        $dRequest->fecha_nacimiento = Carbon::parse($respuesta['fecha_nacimiento'])->format('Y-m-d');

        $dRequest->nro_afiliado = $respuesta['numero_afi'];
        $dRequest->user_id = Auth::user()->id;
    } catch (\SoapFault $e) {
        echo "ERROR EN EL SOAP";
        echo "<br />";
        echo $e->faultcode;
        echo "<br />";
        echo $e->faultstring;
    }
    catch(Exception $e){
        echo "Error Exception luego del Webservice";
    }

    $d = new D;
    $d = $this->fillD($dRequest, $d);
    $d->save();

    Session::flash('message-success', 'Archivo guardado satisfactoriamente.');
    return redirect()->route('d.index');
}

In this case $ answer has the values of the webservice and I assign them to the request, but this does not work if I do not do the following

 private function fillD(DRequest $dRequest, D $d)
{
    $d->user_id = $dequest->user_id;
    $d->firma_localidad_id = $dRequest->firma_localidad_id;
    $d->apellido_nombre = $dRequest->apellido_nombre;
    $d->fecha_nacimiento = $dRequest->fecha_nacimiento;
    $d->edad = $dRequest->edad;
    $d->sexo = $dRequest->sexo;
    $d->anio = $dRequest->anio;
    $d->puntaje_fim = $dRequest->puntaje_fim;
    $d->firma_profesional = $dRequest->firma_profesional;
    $d->firma_fecha = $dRequest->firma_fecha;


    return $d;
}

The Webservice has only the surname_name and gender fields, the rest are from the table.

The question is how you can assign to the Request values to fields that are not in the form and that I get them through a webservice, without throwing me the error, that way I can save it like this

Noticia::create($noticiaRequest->all());

And in a line of code I resolve the issue of saving

THANK YOU in advance and sorry if I could not explain it well

    
asked by desarrollosTELLO 20.01.2018 в 14:40
source

1 answer

0

The answer is that you must first build an array that contains all the data; if part of the data is provided by the array A and part of the array B.

What we need is to make a merge:

public function store(Request $request, D $d){
...
/* aca use ->except para excluir el token csrf, si no usas token csrf puedes usar ->all */
$request_final = array_merge($request->except(['_token']), $array_de_webservice);
/* esto producira un array de todos los campos de array A + array B */
/* finalmente creamos */
Noticia::create($request_final);

Greetings I hope you find it useful

    
answered by 20.01.2018 в 14:55