简体   繁体   中英

Update with array laravel 5.2

I need to update my data table.

From my input name , need all the fields with to be updated with the value of the latter coming from ask_id form:

{!! Form::open(array('url'=>array('/formato/'.$formato->id.'/update'), 'method'=>'PATCH')) !!}
        <div class="row">
            @foreach($pregunta as $preguntas)
            <div class="col-md-4">
                <div class="form-group">
                    {!! Form::hidden('ask_id[]', $preguntas->id) !!}
                    {!! Form::text('name[]', $preguntas->name, ['class'=>'form-control']) !!}
                </div>
            </div>
            @endforeach
        </div>
        <div class="row">
            <div class="col-md-4">
                <button class="btn btn-block btn-success">
                    Actualizar
                </button>
            </div>
        </div>
    {!! Form::close() !!}

Controller:

public function formatoUpdate(Request $request, $id)
    {

        $item = $request->input('name');
        $ask = $request->input('ask_id');

        foreach ($ask as $ask_id)
        {
            $pregunta = ask::find($ask_id);
            $pregunta->update($item);
        }

        return redirect('/formato');
    }

I am not sure what pregunta is but you could try:

$pregunta->name = $item; //name is the field in your table
$pregunta->save();

use foreach in formatoUpdate() like this :-

foreach ($ask as $key => $ask_id)
        {
            $pregunta = ask::find($ask_id);
            $pregunta->update($item[$key]);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM