How to instantiate one model inside the other in the view?

Asked

Viewed 242 times

0

Error: Property [id] does not exist on this Collection instance.

MIGRATION DIVIDE

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDividasTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dividas', function (Blueprint $table) {


            $table->increments('id');
            $table->string('numdoc')->nullable();

            $table->integer('cliente_id')->unsigned();
            $table->foreign('cliente_id')->references('id')->on('clientes');

            $table->integer('devedor_id')->unsigned();
            $table->foreign('devedor_id')->references('id')->on('devedors');

            $table->date('data_emissao');
            $table->date('data_debito');

            $table->integer('meses_atraso')->default(0);
            $table->integer('multa')->default(0);
            $table->double('valor_multa')->default(0);

            $table->double('taxa')->default(0);
            $table->double('valor');
            $table->double('valor_recebido')->default(0);
            $table->double('valor_aberto')->default(0);
            $table->double('valor_total_corrigido')->default(0);

            $table->integer('user_id');
            $table->string('status')->default('pendente');
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('dividas');
    }
}

MIGRATION TITLE

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTitulosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('titulos', function (Blueprint $table) {

            // cadastro padrão
            $table->increments('id');
            $table->string('referencia');

            $table->integer('tipo_id')->unsigned();
            $table->foreign('tipo_id')->references('id')->on('titulo_tipos');

            $table->integer('divida_id')->unsigned();
            $table->foreign('divida_id')->references('id')->on('dividas');


            $table->integer('user_id');

            $table->date('vencimento_titulo')->nullable();
            $table->double('valor')->nullable();
            $table->timestamps();

            // fim cadastro padrão




        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('titulos');
        Schema::dropIfExists('devedors');
        //Schema::dropIfExists('clientes');

    }
}

MODEL DEBT

 public function titulo()
    {
        return $this->hasMany('App\Models\Titulo');
    }

MODEL TITLE

public function divida()
    {
        return $this->belongsTo('App\Models\Divida', 'divida_id');
    }
  • Can explain better?

  • I would like to access fields of the title table in debt

  • Ex.: $divide->security->value

  • Put the Models in the question

2 answers

1


I would advise you not to use foreign keys in the bank, it is a responsibility of the front to maintain integrity, in addition to that if you plan to scale your bank horizontally it would make this impossible and much this task. use directly in your model and not in the bank.

Do your Migration just like this:

$table->integer('tipo_id');

And in your model of titles do:

 public function tipos()
    {
        return $this->belongsTo('App\Models\Tipo', 'id', 'tipo_id);
    } 

I believe that its title has only one type, and a type can have several titles in the type model:

 public function titulos()
    {
        return $this->hasMany('App\Models\Titulos', 'tipo_id', 'id');
    } 

Some good articles on:

https://medium.com/@hernandev/always-use-foreign-no-mysql-e24b4e2187f5

https://medium.com/@lemesdaniel/o-porqu%C3%AA-de-eu-n%C3%A3o-use foreign-no-mysql-2e0179af4b2c

0

The hasMany relationship, represents a relationship of one for many.

See https://laravel.com/docs/5.8/eloquent-relationships#one-to-Many

In your case a debt many securities.

Note:
The method in the Debt model should be called titles and not title because it represents a collection of titles and not a title.

In this case you are trying to get "value" from the collection, which will not work.

Relationships are returned in the form of a Collection

See https://laravel.com/docs/5.8/collections

Example to get the first collection item you can do.

$divida->titulos->first()->valor

Example to check if there are debt-linked securities.

$divida->titulos->isEmpty()

Last note, refer to the Eager Loading documentation to avoid problems with N+1 database queries

See

https://laravel.com/docs/5.8/eloquent-relationships#Eager-loading

Browser other questions tagged

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