samedi 30 juin 2018

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></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><button class="btn btn-outline-primary" onclick="location.href=''">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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire