Laravel 5 Eloquent View Blade with Foreign Key?

0

I get the following error:

  

Trying to get property 'name' of non-object

I have a list of Suppliers and Payments, and I am using the following syntax to retrieve the Vendor name through the foreign key of the payments.

<td>{{$pagamento->fornecedor_id->nome}}</td>

Payments table

public function up()
{
    Schema::create('pagamentos', function (Blueprint $table) {
        $table->increments('id');
        $table->date('data_pag');
        $table->decimal('valor');            
        $table->string('plano_contas', 20);
        $table->integer('fornecedor_id')->unsigned()->nullable();      
        $table->foreign('fornecedor_id')->references('id')->on('fornecedores')
            ->onUpdate('cascade')
            ->onDelete('set null');
        $table->timestamps();
    });
}

Suppliers Table

public function up()
{
    Schema::create('fornecedores', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nome', 55);
        $table->string('cnpj', 18)->unique();
        $table->string('endereco', 44);            
        $table->integer('numero');                
        $table->string('cep', 9);            
        $table->string('complemento', 44)->nullable();            
        $table->string('bairro', 44);            
        $table->string('celular', 14)->nullable();              
        $table->string('telefone', 13);              
        $table->string('cidade', 44);              
        /*$table->integer('city_id')->unsigned()->nullable();      
        $table->foreign('city_id')->references('id')->on('cities')
            ->onUpdate('cascade')
            ->onDelete('set null');  */      
        $table->string('estado', 2);                      
        $table->string('email', 44)->unique();                      
        $table->string('inscri_estadual', 44)->unique()->nullable();                      
        $table->string('inscri_municipal', 44)->unique()->nullable();                         
        $table->timestamps();
    });


}

Controller Payments

public function index(){
    $pagamentos = Pagamento::all();
    $fornecedores = Fornecedor::all();

    return view('pagamentos.pagamento', 
       compact('pagamentos','fornecedores'));         
}

Model Payment

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Fornecedor;

class Pagamento extends Model
{
    //
    protected $table = 'pagamentos';

    protected $fillable = [
        'data_pag', 'valor', 'fornecedor_id','plano_contas',
    ];



    public function fornecedores(){
        return $this->belongsTo('App\Fornecedor');
    }
}

Model Suppliers

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Pagamento;


class Fornecedor extends Model
{
    //
    protected $table = 'fornecedores';

    protected $fillable = [
        'nome', 'cnpj', 'endereco', 'numero', 'cep', 'complemento', 'bairro', 'celular', 'telefone', 'cidade', 'estado', 'email', 'inscri_estadual', 'inscri_municipal',
    ];


    public function pagamento(){
        return $this->hasMany('App\Pagamento');
    }
}
{{ dd($pagamento) }}
  

@foreach($pagamentosas$pagamento){{dd($pagamentos)}}<trclass="item{{$pagamentos->id}}">
                                <td>{{$pagamento->id}}</td>
                                <td>{{$pagamento->data_pag}}</td>
                                <td>{{$pagamento->valor}}</td>
                                <td>{{$pagamento->fornecedor_id->nome}}</td>
                                <td>{{$pagamento->plano_contas}}</td>
                                <td><button class="btnOpenUpdatePag btn btn-
info" data-id="{{$pagamento->id}}" data-data_pag="{{$pagamento->data_pag}}" 
data-valor="{{$pagamento->valor}}" data-pessoa="{{$pagamento->fornecedor_id-
>nome}}" data-plano_contas="{{$pagamento->plano_contas}}"> <span class="fa 
fa-pencil"></span></button>'
                                    <button class="delete-modal btn btn-danger" data-id="{{$pagamento->id}}" data-data_pag="{{$pagamento->data_pag}}" data-valor="{{$pagamento->valor}}" data-pessoa="{{$pagamento->fornecedor_id->nome}}" data-plano_contas="{{$pagamento->plano_contas}}" ><span class="fa fa-trash"></span></button>
                                </td>
                            </tr>
                            @endforeach

Vendor Id Payments Making the Correct Relationship!

  

PRINTTABLESUPPLIERS

  

    
asked by anonymous 05.02.2018 / 16:56

1 answer

0

Well, as I said in the comments, the way to access the attribute is incorrect. try it this way (I put the table so to show in print):

   <table>
    <thead>
        <tr>
            <th>Id</th>
            <th>Data Pag</th>
            <th>Valor</th>
            <th>Fornecedor</th>
            <th>Plano Contas</th>
        </tr>
    </thead>
    @foreach($pagamentos as $pagamento)
        <tr data-data_pag="{{$pagamento->data_pag}}" data-valor="{{$pagamento->valor}}"
            data-pessoa="{{$pagamento->fornecedor()->get()->first()->nome}}"
            data-plano_contas="{{$pagamento->plano_contas}}">
            <td>{{$pagamento->id}}</td>
            <td>{{$pagamento->data_pag}}</td>
            <td>{{$pagamento->valor}}</td>
            <td>{{$pagamento->fornecedor()->get()->first()->nome}}</td>
            <td>{{$pagamento->plano_contas}}</td>
        <tr>
    @endforeach
</table>

    
05.02.2018 / 18:00