Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 862
Victor Gonçalves Avatar asked Oct 28 '22 09:10

Victor Gonçalves


2 Answers

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

like image 140
Renoir Reis Avatar answered Nov 15 '22 07:11

Renoir Reis


$ordem->clientes is array, display them like this

     @foreach($ordens as $ordem)
                <?php //dd($ordem->clientes->nome); ?>
        <tr>
            <th scope="row"></th>
            <td>{{$ordem->id}}</td>
            @foreach($ordem->clientes->toArray() as $client)
                <td>{{$client['nome']}}</td>
            @endforeach
            <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
like image 36
Hussein Avatar answered Nov 15 '22 06:11

Hussein