I'm trying to create a simple% category of categories and sub categories where a category can have several subcategories but a subcategory can have only one category. In my migration I made my tables as follows:
Schema::create('sub_categorias', function (Blueprint $table) {
$table->increments('CdSubCategoria');
$table->integer('CdCategoria')->unsigned()->index();
$table->string('NmSubCategoria');
$table->string('DscSubCategoria');
$table->integer('FlgPontua');
$table->integer('QtdPontos');
$table->integer('MaxPontosPorSubCategoria');
$table->timestamps();
$table->softDeletes();
$table->foreign('CdCategoria')
->references('CdCategoria')
->on('categorias');
});
And my category table:
Schema::create('categorias', function (Blueprint $table) {
$table->increments('CdCategoria');
$table->string('NmCategoria', '50');
$table->string('DscCategoria', '255');
$table->timestamps();
$table->softDeletes();
});
Already in my Category Model I did:
use SoftDeletes;
protected $fillable = ['CdCategoria','NmSubCategoria', 'DscSubCategoria', 'FlgPontua', 'QtdPontos', 'MaxPontosCategoria'];
protected $primaryKey = 'CdSubCategoria';
protected $dates = ['deleted_at'];
public function categoria()
{
return $this->belongsTo('App\Categoria');
}
And of subcategories:
use SoftDeletes;
protected $fillable = ['NmCategoria', 'DscCategoria'];
protected $primaryKey = 'CdCategoria';
protected $dates = ['deleted_at'];
public function subCategoria()
{
return $this->hasMany('App\SubCategoria');
}
With this all my controller sends to a lisSubCategories view, where I want to list the subcategories and show the parent category name of it, in this way:
$subCategoria->categoria->NmCategoria
When doing this command together with CRUD
the error page is returned to me describing the error:
Trying to get non-object property (View: ... \ views \ SubCategory \ listSubCategory.blade.php)