Problems with the return of the hasMany relational

Asked

Viewed 58 times

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

1 answer

1


You have to carry the relationship:

$lotes  = LoteProduto::with('produto')->where('CdProduto', $id)->get();
foreach($lotes as $lote):
    $lote->produto ...
endforeach;

and that returns a list, you have to go through with for.

  • But to access how I do ?

  • I made this $lots->product = Product::find($id); however on the page it identifies that it already has lots created and does not have

  • 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)

  • I changed it was something else

  • You’re still in trouble

  • Place the model Loteproduct!

  • edited the question

  • Give a var_dump in this variable batches and paste in the question and another will have data????

  • There’s also a problem that shouldn’t be too much???

  • No, precisely the problem happens by this, because I have no lot, when I have lot goes normal

  • No, every product has its lot

  • or better each batch has its product and each product has its batches

  • Within a batch have how many products? And one product can be in multiple batches?

  • Within a lot, there is only one product, and within a product can have several lots

  • It returns a precise list of a for there

  • The command should be dd($lots)

Show 12 more comments

Browser other questions tagged

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