-1
I am trying to save Checklist data from protocol, but the following error is occurring at the time of saving:
The expectation is that this happens:
In the description of the error it says that the type inserted does not hold the storage in the database, that is, the insertion is a line with all the data.
Follows the database structure:
Follow the details of the database fields:
Follows the printing of variables:
Model Code Projectocontroller.php as the registration methodChecklistProtocolo
public function cadastroChecklistProtocolo(Request $request)
{
//dd($request->request);
$dados = $request->only(['item', 'item_descricao_id', 'sim_nao', 'nao_atende', 'dt_validade', 'pagina_documento', 'observacao']);
//$checklistsProtocolos = new ChecklistProtocolo;
$checklistsProtocolos = ChecklistProtocolo::create($dados);
$checklistsProtocolos->save($dados);
//echo '<pre>';
//dd($dados); //imprimir as variaveis na tela.
$response = true;
if($response)
{
return redirect()
->route('projeto.edita', $request->projeto_id)
->with('success',"Sucesso ao atualizar os Checklists do Protocolo");
}else
{
return redirect()
->back()
->with('error',"Erro ao atualizar o Checklists do Protocolo");
}
}
Code Model Checklistprotocol.php:
<?php
namespace App Models;
use Illuminate Database Eloquent Model; use DB; use Softdeletes;
Checklistprotocol extends Model { protected $table = "checklist_protocol";
protected $primaryKey = ['projeto_id', 'modelo_id','itens_descricao_id'];
public $incrementing = false;
public $timestamps = false;
protected $fillable = ['projeto_id', 'modelo_id','item','item_descricao_id','sim_nao', 'nao_atende','dt_validade','pagina_documento','observacao'];
protected $casts = [ 'item' => 'array', 'item_descricao_id'=> 'array','sim_nao' => 'array', 'dt_validade'=> 'array',
'pagina_documento'=> 'array', 'observacao'=> 'array', 'nao_atende'=> 'array'];
/*
public function checklistProtocolo()
{
return $this->belongsTo(ChecklistProtocolo::class, 'projeto_id','modelo_id', 'itens_descricao_id');
} */
//Este método salva os dados do Checklist do Protocolo
public function salvar(ChecklistProtocolo $checklistProtocolo) : Array
{
$checklistProtocolo = $this->save();
if($checklistProtocolo){
return[
'success' => true,
'message' => 'Sucesso ao cadastrar'
];
}
else{
return[
'success' => false,
'message' => 'Falha ao cadastrar'
];
}
}
The display code edits.blade.php from Checklist:
@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[]" value="{{$checklistProtocolo->item_descricao_id}}">
<td><input type="checkbox" id="sim_nao" name="sim_nao[]" {{$checklistProtocolo->sim_nao == null ? '' : 'checked'}}></td>
<td><input type="checkbox" id="nao_atende" name="nao_atende[]" {{$checklistProtocolo->nao_atende == null ? '' : 'checked'}}></td>
<td><input type="date" id="dt_validade" name="dt_validade[]" value="{{$checklistProtocolo->dt_validade}}"></td>
<td><input type="text" id="pagina_documento" name="pagina_documento[]" value="{{$checklistProtocolo->pagina_documento}}" size ="1"></td>
<td><input type="text" id="observacao" name="observacao[]" value="{{$checklistProtocolo->observacao}}" size ="1" style="width: 300px; height: 60px"></td>
</tr>
@endforeach
In this case I need to save the records separately and in their respective lines. I will test your loop suggestion on each option coming from the request.
– Hugo Leonardo
If I don’t make it, send it like I’m gonna help you.
– André Lins
I updated the description of the question put an image of the expected result.
– Hugo Leonardo
I did so: foreach ($given as $key => $value) { Checklistprotocol :: wherein('item', $value)->create($data); } The insertion error persists.
– Hugo Leonardo