【问题标题】:Laravel 5.6 Trying to get property of non-objectLaravel 5.6 试图获取非对象的属性
【发布时间】:2018-12-09 16:27:13
【问题描述】:

当我尝试回显该值时,我收到一个异常。我用 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

但是当我尝试回显该值时,我收到了一个执行。

$ordem 的 dd()。

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

【问题讨论】:

  • 所有$ordem 都有clientes 吗?你是dd'ing 循环中的第一项,但如果第二项没有clientes 怎么办?你会看到那个异常。
  • 这样使用:optional($ordem-&gt;clientes)-&gt;nome,如果为空$ordem-&gt;clientes
  • 一个订单有多少个客户?如果它有多个,您应该使用$oderm-&gt;clientes-&gt;first()-&gt;name 或遍历所有客户端。
  • 谢谢大家。 select * from ordens_servico select * from clientes where clientes.id in ('1', '2') 急切加载正在尝试查找 ID 为 1 和 2 的客户端。但我有 2 个 $orders对于同一个 $client。使用可选的,名称被回显,但第二个没有。我会弄清楚如何解决这个问题。再次感谢。你给了我一个北方,现在。

标签: php laravel laravel-5 foreach eager-loading


【解决方案1】:

也许你的关系应该被称为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

【讨论】:

  • 使用公共函数 cliente() { return $this->belongsTo('App\Cliente','cliente_id'); }。 Laravel 选择正确。仅搜索一位客户。 select * from clientes where clientes.id in ('1')
【解决方案2】:

$ordem-&gt;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

【讨论】:

  • 感谢您的回复。但我仍然得到“试图获得非对象的属性'nome'”。 client); ?> 返回 TRUE。
【解决方案3】:

您的代码是完美的,您的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 关系使用单数名称,因为它会返回一个模型。例如,您应该使用cliente 关系名称而不是clientes

【讨论】:

    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 2015-09-22
    • 2017-03-20
    • 2014-03-25
    • 2015-01-25
    相关资源
    最近更新 更多