Doubt with relationship One to Many

5

I have two tables, produtos and categorias . In my view of produtos I want to show the category name of that product, not your code.

Category Template Category.php

namespace MagicCommerce\Site\Cadastros;

class Categoria extends \Eloquent {
    public function produtos() {
        return $this->hasMany('Produto');
    }
}

Product Template Product.php

namespace MagicCommerce\Site\Cadastros;

class Produto extends \Eloquent {
    public function categoria() {
        return $this->belongsTo('Categoria');
    } 
}

And in my view I try to call the relationship using:

@foreach ($produtos as $p)
{{ $p->categoria->nome }}
@endforeach

And the following is returned to me:

  

Symfony \ Component \ Debug \ Exception \ FatalErrorException Class   'Category' not found

Access the base data normally, only the relationship does not work. I've also circled dump-autoload , but no success.

    
asked by anonymous 07.01.2014 / 22:19

1 answer

3

Your problem is in class name resolution, add to your controller :

use MagicCommerce\Site\Cadastros\Categoria;

use MagicCommerce\Site\Cadastros\Produto;
    
07.01.2014 / 22:47