当我尝试回显值时,我收到一个例外.我用dd()检查集合,但不是null.
我的模特:
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');
}
}
Run Code Online (Sandbox Code Playgroud)
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');
}
}
Run Code Online (Sandbox Code Playgroud)
OrdemServicoController:
public function index()
{
$ordens = OrdemServico::with('clientes')->get();
return view('home',compact('ordens'));
}
Run Code Online (Sandbox Code Playgroud)
零件视图主页:
<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 …Run Code Online (Sandbox Code Playgroud)