How do I resolve the "call to Undefined method Parameterbag::save() " error?

Asked

Viewed 220 times

0

I am trying to save Checklist data from protocol, but the following error is occurring at the time of saving:

Call to Undefined method Symfony Component Httpfoundation Parameterbag::save() inserir a descrição da imagem aqui

The following image shows the protocol’s Checklist model and the second image is the request variable debug. This debug shows how the data is structured. inserir a descrição da imagem aqui
Debug of the variable "request", data from the Checklist protocol modal. Yellow marked data are Checklist variables and red marked data is the id of the array item. inserir a descrição da imagem aqui

Loop responsible for displaying protocol Checklist data:

   @foreach($checklistsProtocolos as $checklistProtocolo)

                                <tr>
                                    <td><input type="text" class="form-control"  id="item" name="item[]" value="{{$checklistProtocolo->item}}"  size ="2"></td>
                                    <td>{{$checklistProtocolo->descricao_item}}</td>   
                                    <input type="hidden"          id="item_descricao_id"  name="item_descricao_id[{{$checklistProtocolo->item}}][]" value="{{$checklistProtocolo->item_descricao_id}}">
                                    <td><input type="checkbox"    id="sim_nao"            name="sim_nao[{{$checklistProtocolo->item}}][]"    {{$checklistProtocolo->sim_nao == null ? '' : 'checked'}}></td>
                                    <td><input type="checkbox"    id="nao_atende"         name="nao_atende[{{$checklistProtocolo->item}}][]" {{$checklistProtocolo->nao_atende == null ? '' : 'checked'}}></td>
                                    <td><input type="date"        id="dt_validade"        name="dt_validade[{{$checklistProtocolo->item}}][]" value="{{$checklistProtocolo->dt_validade}}"></td>
                                    <td><input type="text"        id="pagina_documento"   name="pagina_documento[{{$checklistProtocolo->item}}][]" value="{{$checklistProtocolo->pagina_documento}}" size ="1"></td>
                                    <td><input type="text"        id="observacao"         name="observacao[{{$checklistProtocolo->item}}][]" value="{{$checklistProtocolo->observacao}}" size ="1" style="width: 300px; height: 60px"></td>
                                </tr>

                            @endforeach 



Controller protocol Checklist registration method:

 public function cadastroChecklistProtocolo(Request $request)
        {

            dd($request->request);

           //Deletar a tabela de checklist_protocolo
           $checklistsProtocolos = ChecklistProtocolo::where('projeto_id','=', $request->projeto_id)->delete();

           //Recebe os dados do modal Checklist Protocolo
            $checklistProtocolo =  $request->request;

            $checklistProtocolo->save();//está ocorrendo um problema no momento de salvar os dados

        }
  • Are you sure that $checklistsProtocolo is a Model?

1 answer

0

To update a template using save, first you must recover it, set the attributes you want to update or create and then call the save method.

In your case you did not recover the template, only the request, which is saved in the variable $checklistProtocolo and tried to save her.

Try it like this:

public function cadastroChecklistProtocolo(Request $request)
{
    // Deletar a tabela de checklist_protocolo
    $checklistsProtocolos = ChecklistProtocolo::where('projeto_id', $request->projeto_id)->delete();

    // Recebe os dados do modal Checklist Protocolo
    $dados = $request->all();

    $checklistsProtocolo = new ChecklistProtocolo;

    $checklistProtocolo->save($dados);
} 

Another way would be to use the create

public function cadastroChecklistProtocolo(Request $request)
{
    // Deletar a tabela de checklist_protocolo
    $checklistsProtocolos = ChecklistProtocolo::where('projeto_id', $request->projeto_id)->delete();

    $checklistsProtocolo  = ChecklistProtocolo::create($request->all());
}

Reference

  • Hello, @Dobrychtop I made the change as recommended, the error did not appear. But the data is not being saved in the database.

  • @Ruama, use dd($request->all()); and check the result, also see if the fields are released for mass assignment in your model, within the attribute protected $fillable = ['id','campo','outro_campo'];

  • Oi, @Dobrychtop, eu inserir o seguinte código, mas o erro permanece: protected $fillable = ['projeto_id', 'modelo_id','itens_descricao_id','sim_nao', 'nao_atende','dt_validade','pagina_documento','observacao'];

  • When I tried the second way using create, the following error occurred: Array to string Conversion (SQL: Insert into checklist_protocolo (projeto_id, modelo_id, sim_nao, nao_atende, dt_validade, pagina_documento, observacao) values (12, 7, on, on, 2018-11-21, 1, test))

  • @Ruama, you intend to record a repeat line projeto_id, modelo_id for each of your itens? If yes you would need to use one foreach for each of them, manually formatting their fields, such as dados['projeto_id'] = $projeto_id;, and then use the create inside that loop for each of your lines.

  • However this would not be the correct approach, you should have a table with single fields, projeto_id, modelo_id, and a table that records the items of this project with an N:N relation in its model, so the project would not be recorded repeatedly, and would not matter how many items or dates it has. But this approach already escapes from the initial question, and even goes through the formal rules of relational database normalization .

  • Hello, thank you for the return. I re-did the method and it was as follows:

  • public Function cadastroChecklistProtocolo(Request $request) {&#xA; $dados = $request->all(); $checklistsProtocolo = new ChecklistProtocolo;&#xA; foreach ($dados as $dado) { $checklistsProtocolo->projeto_id = $dado['projeto_id'];$checklistsProtocolo->modelo_id = $dado['modelo_id']; $checklistsProtocolo->item = $dado['item']; $checklistsProtocolo->item_descricao_id = $dado['item_descricao_id'];&#xA;$checklistsProtocolo->pagina_documento = $dado['pagina_documento'] $checklistsProtocolo->observacao = $dado['observacao']; $checklistsProtocolo->save();}

  • But, the following error occurred: Illegal string offset 'project_id'

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.