这是blade.php
<h1> Cadastro de clientes </h1>
<hr/>
@if(count($cliente)>0)
<ul>
@foreach($cliente as $cliente)
<li>{{$nome->nome}}</li>
<li>{{$endereco->endereco}}</li>
<li>{{$email->email}}</li>
<li>{{$celular->celular}}</li>
@endforeach
</ul>
@else
<h4>Não há clientes</hr>
@endif
这是模型:
<?php
namespace App;
Use Illuminate\Database\Eloquent\Model;
class Cliente extends Model{
protected $table = 'cliente';
public $timestamps = false;
}
这是控制器:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Cliente;
class CadCliController extends Controller {
public function cadCli() {
$cliente = Cliente::all();
$array = array('cliente'=>$cliente);
return view('cadCli', $array);
}
我无法在foreach上显示表格。继续显示变量nome未定义的错误。能否请你帮忙?............................................. .......................................
答案 0 :(得分:1)
您应该只能更改@foreach循环。在典型的@foreach循环中,您将循环访问一组记录以显示有关每个单个记录的信息。这就是为什么通常会看到@foreach($records as $record)
的原因。
我不确定您所用语言的'cliente'单数形式,因此我使用了client
。然后,您正在从各个客户记录中调用nome
,endereco
等。看看这@foreach是否有帮助:
@foreach($cliente as $client)
<li>{{$client->nome}}</li>
<li>{{$client->endereco}}</li>
<li>{{$client->email}}</li>
<li>{{$client->celular}}</li>
@endforeach
答案 1 :(得分:0)
首先,如果要返回一组元素,请与您使用的变量名称保持一致(使用clientes
而不是cliente
)
class CadCliController extends Controller {
public function cadCli() {
$clientes = Cliente::all();
return view('cadCli', ['clientes' => $clientes]);
}
}
现在,在您看来,如果您在@foreach
语句中看到,则说明您正在这样做:
...
@if(count($cliente)>0)
<ul>
@foreach($cliente as $cliente)
<li>{{$nome->nome}}</li> // <---------
<li>{{$endereco->endereco}}</li> // <---------
<li>{{$email->email}}</li> // <---------
<li>{{$celular->celular}}</li> // <---------
@endforeach
</ul>
@else
...
如您所见,这些变量($nome
,$endereco
等)不存在,因为您将$clientes
数组的每一项定义为$cliente
,所以我认为这些是每个对象的属性:
...
@if(count($cliente)>0)
<ul>
@foreach($clientes as $cliente)
<li>{{$cliente->nome}}</li> // <---------
<li>{{$cliente->endereco}}</li> // <---------
<li>{{$cliente->email}}</li> // <---------
<li>{{$cliente->celular}}</li> // <---------
@endforeach
</ul>
@else
...