Search code examples
phplaravellaravel-5foreacheager-loading

Laravel 5.6 Trying to get property of non-object


When i try echo the value i receive an exeception. I check the collection with dd() and is not null.

My Models:

Cliente:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cliente extends Model
{
    protected $table = 'clientes';

    public function ordens() {
        return $this->hasMany('App\OrdemServico','cliente_id');
    }
}

OrdemServico:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrdemServico extends Model
{
    protected $table = 'ordens_servico';

    public function clientes() {
        return $this->belongsTo('App\Cliente','id');
    }
}

OrdemServicoController:

public function index()
{

    $ordens = OrdemServico::with('clientes')->get();

    return view('home',compact('ordens'));

}

Part View Home:

             <table class="table">
                    <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">Nome Cliente</th>
                        <th scope="col">Modelo</th>
                        <th scope="col">Status O.S</th>
                    </tr>
                    </thead>
                @foreach($ordens as $ordem)
                    <?php //dd($ordem->clientes->nome); ?>
                    <tr>
                        <th scope="row"></th>
                        <td>{{$ordem->id}}</td>
                        <td>{{$ordem->clientes->nome}}</td>
                        <td></td>
                        <td></td>
                        <td><button class="btn btn-outline-primary" onclick="location.href='{{ route('ordem_servico.show',1) }}'">Mostrar mais</button></td>
                    </tr>
                @endforeach

                    </tbody>
                </table>

When i

<?php dd($ordem->clientes->nome); ?>

I receive:

dd() return

But when i try echo the value, i receive a execpetion.

dd() of $ordem.

https://gist.github.com/vgoncalves13/8140a7a1bf2cb85fe4d16da20ea34acc


Solution

  • Perhaps your relationship should be called cliente() assuming a order only belongs to one App\Cliente... and you should pass the third argument which is the other_key... you can avoid this type of error by using english (since laravel would search for customer_id and order_id)

    Try the following code

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Cliente extends Model
    {
        protected $table = 'clientes';
    
        public function ordens() {
            return $this->hasMany('App\OrdemServico','cliente_id');
        }
    }
    

    and

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class OrdemServico extends Model
    {
        protected $table = 'ordens_servico';
    
        public function cliente() {
            return $this->belongsTo('App\Cliente', 'id', 'cliente_id');
        }
    }
    

    Reference: https://laravel.com/docs/5.6/eloquent-relationships#one-to-many