Problems with relationship one to many laravel

2

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)

    
asked by anonymous 10.09.2016 / 05:25

1 answer

3

In Eloquent , there is a default in the relationship keys, which is a non-binding standard, which is a nome de tabela , underscore and chave , example : categoria_id .

"https://laravel.com/docs/5.3/eloquent#introduction">Equick can be configured with field names in their own way, but this implies informing Equick what are the keys and field names.

Translation

1: 1

$this->hasOne(relacao, chave estrangeira, primary key);

N: 1

$this->belongsTo(relação, chave estrangeira local, primary key da relação); 

1: N

$this->hasMany(relação, chave estrangeira da relação, primary key local);

N: N

$this->belongsToMany('relacao', 'nome da tabela pivot', 'key ref. local model em pivot', 'key ref. relação em pivot')

This was all just the introduction to solve the problem of the question, so by reconfiguring the relationships, as explained does not follow the convention needs to be specified in your Eloquent Model .

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','CdCategoria','CdCategoria');
}
use SoftDeletes;

protected $fillable = ['NmCategoria', 'DscCategoria'];

protected $primaryKey = 'CdCategoria';

protected $dates = ['deleted_at'];

public function subCategoria()
{
    return $this->hasMany('App\SubCategoria','CdCategoria','CdCategoria');
}

In your specific case it was easy to relate, often the keys have different names and do extra work in configuring and maintaining code.

To search for relationship information use with ('link_name-> ; method name ') to load eager loaded of relation:

Example:

$subCategoria = SubCategoria::with('categoria')->get();

and this summarizes in the code below that needs to be executed:

$subCategoria->categoria->NmCategoria
    
10.09.2016 / 16:09