Skip to main content
2 de 2
adicionou 69 caracteres ao conteúdo
André Cabral
  • 819
  • 13
  • 29

Como instanciar uma model dentro da outra na view?

Erro: Property [id] does not exist on this collection instance.

MIGRATION DIVIDA

<?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 TITULO

<?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 DIVIDA

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

MODEL TITULO

public function divida()
    {
        return $this->belongsTo('App\Models\Divida', 'divida_id');
    }
André Cabral
  • 819
  • 13
  • 29