0
i want to write a code so that the user chooses an ID in a combobox and specific information is shown in HTML, but I need to do an if to know if the chosen provider is A, B or C, but I can’t return the variable inside the if to pass to view.
follows Controller:
$id = DB::table('rcis')->pluck('rci_id');
$rciid = $request->input('idrci');
$teste = DB::table('rcis')->select('fornecedor_escolhido')->where('rci_id', '=', $rciid)->get();
if($teste=='[{"fornecedor_escolhido":"A"}]'){
$rci = DB::table('rcis')
->join('colaboradors', 'rcis.fk_colaborador', '=', 'colaboradors.colaborador_id')
->join('centrocustos', 'rcis.fk_centrocusto', '=', 'centrocustos.centrocusto_id')
->join('status_rcis', 'rcis.fk_status_rci', '=', 'status_rcis.status_rci_id')
->join('fornecedor_detalhes', 'fornecedor_detalhes.fk_rcid', '=', 'rcis.rci_id')
->select('rcis.observacoes',
'rcis.data_rci',
'colaboradors.nome_colab',
'centrocustos.cod_centrocusto',
'status_rcis.descricao_status',
'rcis.fornecedor_escolhido',
'fornecedor_detalhes.valortotalcompra',
'fornecedor_detalhes.prazo_entrega')
->where('rci_id', '=', $rciid)
->where('rcis.fornecedor_escolhido','=', 'A')
->wherecolumn('rcis.fornecedor_escolhido','=', 'fornecedor_detalhes.letra')
->get();
}
elseif($teste=='[{"fornecedor_escolhido":"B"}]'){
$rci = DB::table('rcis')
->join('colaboradors', 'rcis.fk_colaborador', '=', 'colaboradors.colaborador_id')
->join('centrocustos', 'rcis.fk_centrocusto', '=', 'centrocustos.centrocusto_id')
->join('status_rcis', 'rcis.fk_status_rci', '=', 'status_rcis.status_rci_id')
->join('fornecedor_detalhes', 'fornecedor_detalhes.fk_rcid', '=', 'rcis.rci_id')
->select('rcis.observacoes',
'rcis.data_rci',
'colaboradors.nome_colab',
'centrocustos.cod_centrocusto',
'status_rcis.descricao_status',
'rcis.fornecedor_escolhido',
'fornecedor_detalhes.valortotalcompra',
'fornecedor_detalhes.prazo_entrega')
->where('rci_id', '=', $rciid)
->where('rcis.fornecedor_escolhido','=', 'B')
->wherecolumn('rcis.fornecedor_escolhido','=', 'fornecedor_detalhes.letra')
->get();
}
elseif($teste=='[{"fornecedor_escolhido":"C"}]'){
$rci = DB::table('rcis')
->join('colaboradors', 'rcis.fk_colaborador', '=', 'colaboradors.colaborador_id')
->join('centrocustos', 'rcis.fk_centrocusto', '=', 'centrocustos.centrocusto_id')
->join('status_rcis', 'rcis.fk_status_rci', '=', 'status_rcis.status_rci_id')
->join('fornecedor_detalhes', 'fornecedor_detalhes.fk_rcid', '=', 'rcis.rci_id')
->select('rcis.observacoes',
'rcis.data_rci',
'colaboradors.nome_colab',
'centrocustos.cod_centrocusto',
'status_rcis.descricao_status',
'rcis.fornecedor_escolhido',
'fornecedor_detalhes.valortotalcompra',
'fornecedor_detalhes.prazo_entrega')
->where('rci_id', '=', $rciid)
->where('rcis.fornecedor_escolhido','=', 'C')
->wherecolumn('rcis.fornecedor_escolhido','=', 'fornecedor_detalhes.letra')
->get();
}
return view ('/escolherci', ['id'=>$id, 'rci'=>$rci]);
}
and the view.:
<h3>Escolha uma chave estrangeira para selecionar uma RCI</h3>
<!--ID DROPDOWN-->
<label for="idrci">Chave estrangeira: </label>
<select class="form-control" type="text" name="idrci" id="idrci">
@foreach ($id as $idr)
{
<option value="{{ $idr }}">{{ $idr }}</option>
}
@endforeach
</select><br><br>
<input type="submit" value="Salvar"> </button>
</form>
@foreach($rci as $rc)
Requisitante: {{$rc->nome_colab}}<br><br>
Centro de custo: {{$rc->cod_centrocusto}}<br><br>
Descrição: {{$rc->observacoes}}<br><br>
Status RCI: {{$rc->descricao_status}}<br><br>
Valor total da compra: {{$rc->valortotalcompra}}<br><br>
Prazo entrega: {{$rc->prazo_entrega}}<br><br>
@endforeach
in the HTML form is only for the controller to take the ID target=_self to show on the same page. I need to return this variable within ifs $rci to view
You are only returning the $rci variable and leaving the controller. You must take the Return $rci out of ifs for the controller to reach the last line of the Return
– Ademilson Santana da Silva
this was only one of the attempts I made, the problem is that I can not catch the variable inside the if to play in the view
– Julia Zuin
at the end of each if type dd($rci) to see if he is entering any if
– Ademilson Santana da Silva
made, it returns the values I want in json form.
– Julia Zuin
but do you use the same json format data? try changing the last line to Return view('/choose', ['id'=>$id, 'rci'=>json_decode($rci)])
– Ademilson Santana da Silva
doing that Voce spoke of in the same "Undefined variable: rci" and no, I want to pass the data to HTML that posted , because I will still do more things with the page
– Julia Zuin
dd($rci) before Return view to see. And the $id variable it is finding in Blade?
– Ademilson Santana da Silva
@Ademilsonsantanadasilva made the dd and still same error Undefined variable: rci in the line where I gave the dd, the variable id he picks that is popular the combobox
– Julia Zuin
You want to take the results of rci according to what you select in the first combobox, loading it dynamically?
– Ademilson Santana da Silva
that, user chooses the id and it will load the data in view
– Julia Zuin
if I enter /chose? idrci=2 with the right id, it shows the page as it is to be, but I don’t know why if I enter only /chose not the right one
– Julia Zuin
got it, now it’s clear the error. So, there’s no way to make it not update the page only with php. You have to use ajax in jquery if you want to do without updating the page
– Ademilson Santana da Silva
to avoid the error, before the foreach q vc gives in the $rci on the Lade, put an if(isset($rci)) and put the foreach inside
– Ademilson Santana da Silva
and then Voce can do that by selecting the item of the first combobox, Voce redirects again to that same page by passing the idrci as parameter in the url, the same Voce showed in case it works
– Ademilson Santana da Silva
That is @Ademilsonsantanadasilva worked with if(isset($rci) before foreach, I will update the question, thank you
– Julia Zuin