Laravel 5.6 正在尝试获取 属性 的非对象

Laravel 5.6 Trying to get property of non-object

当我尝试回显该值时,我收到异常。我用 dd() 检查集合并且不为空。

我的模特:

客户:

<?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'));

}

零件视图首页:

             <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>

当我

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

我收到:

dd() return

但是当我尝试回显该值时,我收到一条执行消息。

dd() 共 $ordem。

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

$ordem->clientes是数组,这样显示

     @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

也许你的关系应该被称为 cliente() 假设订单只属于一个 App\Cliente... 你应该传递第三个参数,即 other_key... 你可以通过使用英语来避免此类错误(因为 laravel 会搜索 customer_id 和 order_id)

试试下面的代码

命名空间应用;

使用Illuminate\Database\Eloquent\Model;

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

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

<?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');
    }
}

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

你的代码是完美的,你的 clientes 关系之一可能是空的,你有循环中的代码,导致错误。只需添加 if 检查就可以了

<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>
                    @if ($ordem->clientes)
                        {{$ordem->clientes->nome}}
                    @else
                        'No Client'
                    @endif
                </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>

注意: 始终尝试对 belongsTo 关系使用单数名称,因为它 return 你是一个模型。例如,您应该使用 cliente 关系名称而不是 clientes.