当我尝试回显该值时,我会感到困惑。我用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');
}
}
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); ?>
我收到:
但是当我尝试回显值时,我会得到执行。
$ ordem的dd()。
https://gist.github.com/vgoncalves13/8140a7a1bf2cb85fe4d16da20ea34acc
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
。
答案 3 :(得分:0)
维克多(Victor),客户问题清单(seu problemaestána listagem de clientes),维斯特(tenêttentando acessar)以及财产权所有人(mass este objeto na verdadeéuma lista)。
Osre devdevvocêfazer da seguinte forma,exibida acima,como o @Hussein mostrou,ouentãoda seguinte forma:
@php
$clientes = $ordem->clientes
@enphp
<td>
@forelse( $clientes as $cliente)
{{ $cliente->nome }}
@empty
Nenhum cliente
@endforelse
</td>
恩菲姆(Enfim),请向某人提出上诉,并向某人致以适当的辩护,例如:
<td>{{$ordem->clientes[indice]->nome}}</td>