Laravel 5.6尝试获取非对象属性

5

当我尝试打印值时,收到了一个异常。我使用dd()检查了集合,并且不为null。

我的模型:

客户端:

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

工单:

<?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() 返回值

但是当我尝试打印出这个值时,我收到了一个异常。

$ordem 的 dd()。

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


所有的 $ordem 都有 clientes 吗?你在循环中使用了 dd 来查看第一个项目,但是如果第二个项目没有 clientes,那么你会看到异常。 - Brian Lee
1
$ordem->clientes 为空的情况下,使用如下方式:optional($ordem->clientes)->nome - Mustafa Akçakaya
一个订单有多少个客户?如果超过一个,你应该使用 $order->clientes->first()->name 或循环遍历所有客户。 - Renoir Reis
谢谢大家。选择*从ordens_servico选择*从客户,其中客户.id在('1','2')中渴望加载正在尝试查找ID为1和2的客户。 但是我有两个$orders用于同一个$client。使用可选项,名称被回显,但第二个没有。我会解决这个问题。 再次感谢。你给了我一个方向。 - Victor Gonçalves
3个回答

1
"

$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

谢谢您的回复。但我仍然得到“尝试获取非对象的属性'nome'”。<?php dd($ordem->client); ?> 返回TRUE。 - Victor Gonçalves

1
也许你的关系应该被称为cliente(),假设一个订单只属于一个App\Cliente...并且你应该传递第三个参数,即other_key...你可以通过使用英语来避免这种类型的错误(因为laravel将搜索customer_id和order_id)。请尝试以下代码。

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


使用公共函数cliente() { 返回 $this->belongsTo('App\Cliente','cliente_id'); }。Laravel正确选择。仅搜索一个客户端。select * from clientes where clientes.id in ('1') - Victor Gonçalves

0

你的代码很完美,但是你的一个 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接