Doubt about accessing property in a OneToOne relationship in Laravel

0

Hello,

My question is when accessing a property from a OneToOne relationship. If I use the php operator "- >" it gives error saying that it can not access the property (Trying to get property of non-object Laravel), however, if treating the relation as an array works normally.

I have two tables in the database, one called sellers, and another set of sellers.

Model Sellers:

<?php

namespace App\Pedidos;

use Illuminate\Database\Eloquent\Model;

class Vendedores extends Model
{
    protected $connection = 'Pedidos';
    protected $primaryKey = 'CD_VENDEDOR';

    public function Configuracoes() {
        return $this->hasOne('\App\Pedidos\ConfiguracoesVendedores', 'CD_VENDEDOR', 'CD_VENDEDOR');
    }
}

Model Sales Settings

<?php

namespace App\Pedidos;

use Illuminate\Database\Eloquent\Model;

class ConfiguracoesVendedores extends Model
{
    protected $connection = 'Pedidos';
    protected $primaryKey = 'CD_VENDEDOR';

    public function Vendedor() {
        $this->belongsTo('App\Pedidos\Vendedores', 'CD_VENDEDOR', 'CD_VENDEDOR');
    }
}

No Controller

public function index()
    {
        $dados = Vendedores::orderBy('DS_VENDEDOR', 'ASC')->get();

        return view('pedidos.configuracoesvendedores.index')
            ->with('dados', $dados)
            ->with('title', 'Configurações do vendedor');
    }

In the view, this works correctly.

@foreach($dados as $vendedor)
           <tr>
             <td>{{ $vendedor->DS_VENDEDOR }}</td>
             <td>{{ $vendedor->Configuracoes['X_USA_TABELA'] }}</td>
           </tr>
@endforeach

This does not work.

@foreach($dados as $vendedor)
           <tr>
             <td>{{ $vendedor->DS_VENDEDOR }}</td>
             <td>{{ $vendedor->Configuracoes->X_USA_TABELA }}</td>
           </tr>
@endforeach

Why, when using foreach, should I access the Settings relationship as being array and not as a Collection? For if I use $ data [0] - > Settings-> X_USA_TABELA I can access the value, but if it is inside the foreach not? Is it something I've set wrong somewhere?

Thank you.

    
asked by anonymous 22.08.2018 / 21:20

0 answers