1
I am working on the Batch table of my project where each batch will have its own product and each product may have multiple batches. I set up the whole relationship and now I’m testing access by métodos
. in making $produto->lotes
I can get a satisfactory answer $lotes->produto
You’re not getting anything back.
Man controller
is as follows:
public function mostrar($id){
$lotes = LoteProduto::where('CdProduto', $id)->get();
dd($lotes->produto);
if(Request::wantsJson()){
return $lotes;
}else{
return view('LoteProduto.listLoteProduto', compact('lotes'));
}
}
In my batch research I’ve tried these possibilities:
$lotes = LoteProduto::where('CdProduto', $id)->first();
$lotes = LoteProduto::where('CdProduto', $id)->get()->first();
$lotes = LoteProduto::where('CdProduto', $id)->get()->item;
In my model Produto
have the função
:
public function lotes(){
return $this->hasMany('App\LoteProduto', 'CdProduto', 'CdProduto');
}
that returns me perfectly the lots.
Already in my model Lote
have:
class LoteProduto extends Model
{
use SoftDeletes;
protected $fillable = [
'CdProduto',
'DtFrabricacao',
'DtValidade',
'QtdUnitProduzida'
];
protected $primaryKey = 'CdLote';
protected $dates = ['deleted_at'];
public function produto()
{
return $this->belongsTo('App\Produto','CdProduto','CdProduto');
}
}
When I try to access the $lotes->produto
the following error is shown on the screen:
Errorexception in Loteprodutocontroller.php line 25: Undefined Property: Illuminate Database Eloquent Collection::$product
But to access how I do ?
– Renan Rodrigues
I made this $lots->product = Product::find($id); however on the page it identifies that it already has lots created and does not have
– Renan Rodrigues
Look at the error that gave Undefined Property: Illuminate Database Eloquent Builder::$product (View: C: Users Desktop Computer SGLE-Bakery Projects Resources views Loteproduct listLoteProduct.blade.php)
– Renan Rodrigues
I changed it was something else
– novic
You’re still in trouble
– Renan Rodrigues
Place the model Loteproduct!
– novic
edited the question
– Renan Rodrigues
Give a var_dump in this variable batches and paste in the question and another will have data????
– novic
There’s also a problem that shouldn’t be too much???
– novic
No, precisely the problem happens by this, because I have no lot, when I have lot goes normal
– Renan Rodrigues
No, every product has its lot
– Renan Rodrigues
or better each batch has its product and each product has its batches
– Renan Rodrigues
Within a batch have how many products? And one product can be in multiple batches?
– novic
Within a lot, there is only one product, and within a product can have several lots
– Renan Rodrigues
Let’s go continue this discussion in chat.
– novic
It returns a precise list of a for there
– novic
The command should be dd($lots)
– novic