2
Good morning person.
I created a search that returns requests and when I click opens the products of that request. However I cannot make the products appear as a string, only in array format as in the image. Anyone knows what I have done wrong?
MODEL REQUEST
public function produto()
{
return $this->belongsToMany(Produto::class,'solicitacaodet');
}
PRODUCT MODEL
public function solicitacao()
{
return $this->belongsToMany(Solicitacao::class,'solicitacaodet');
}
CONTROLLHER
public function create(){
// if(Gate::denies('equipamento-create')){
// return view('admin._msg');
// }
$solicitacoes = Solicitacao::All();
$pessoas=DB::table('pessoasolcompvend')
->where('solicitante', '=', 's')
->orderby('pessoa')
->get();
$unidades=DB::table('unidadeadm')
->orderby('unidadeadm')
->get();
$localentrega=DB::table('localentrega')
->orderby('localentrega','asc')
->get();
$caminhos = [
['url'=>'/home','titulo'=>'Inicio'],
['url'=>route('pedidocompra.index'),'titulo'=>'Pedido de Compra'],
['url'=>'','titulo'=>'Create'],
];
return view('compra.pedidocompra.create',compact('solicitacoes','detalhes','pessoas','unidades','localentrega','produtos','centrocustos','caminhos'));
}
VIEW
Solicitation
Date
<?php foreach ($solicitacoes as $solicitacao){?>
<tr style="background-color: #e1e1e1;color: black">
<td id="id"><a href="#"><?php echo $solicitacao->idsolicitacao?></a></td>
<td><?php echo date('d/m/Y', strtotime($solicitacao->data))?></td>
</tr>
<tr>
<td colspan="2">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th></th>
<th>Id</th>
<th>Produto</th>
<th>Qnt</th>
<th>Detalhe</th>
</tr>
</thead>
<tbody>
<?php
$detalhes = $solicitacao->solicitacaodet;
$produtos = $solicitacao->produto;
$centrocusto = $solicitacao->centrocusto;
?>
<?php foreach ($detalhes as $detalhe){?>
<tr>
<td>
<input type="checkbox" />
</td>
<td><?php echo $detalhe->produto_idproduto ?></td>
<td>
<?php
echo $detalhe->produto()->select('produto')->get();
// echo $produto->produto
?>
</td>
<td><?php echo $detalhe->qnt ?></td>
<td><?php echo $detalhe->detalhe ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
I don’t understand, if you already get the products picking up $products = $request->product; why do you foreach in detail? and still get a get at the end?? echo $detail->product()->select('product')->get(); making a foreach on $products, would already bring one product at a time, and remember that ->get() will always return a collection to you
– Gilmar Alonso
solicitacaodet and pivot table. In it will idproduct/idcentrocusto(foreign keys) + qnt + product details, however I am not able to show the product description and cost center only ids. Ex I do a foreach of details and I do not bring description poduto, or I do foreach product and I can not bring the data requecaodet.
– frodrigues
This is how it returns - > [{"PRODUCT":"DUMMY CABLE SET"}].
– frodrigues
but then you have to pass the pivot table and the pivot fields on the relation, something like this: public Function request() { Return $this->belongsToMany(Request::class,'requecaodet')->withPivot('qnt','details'); } and then you foreach with products, and when you want to print the pivot fields you do so: $product->pivot->qnt
– Gilmar Alonso
I am trying but this giving error. Use pivot only in Request or solicitacaodet too? I’m not sure how to mount..
– frodrigues
I’ll post it as an answer, see if it gets clearer
– Gilmar Alonso