0
votes

this is the 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

This is the model:

<?php

namespace App;

Use Illuminate\Database\Eloquent\Model;

class Cliente extends Model{

    protected $table = 'cliente';
    public $timestamps = false;
}

and this is the controller:

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

I'm unable to show the table on foreach. keep showing the error that variable nome is undefined. Could you please help?....................................................................................

2
There is no 'nome' variable passing to your view, where you want your view to grab it from ?Eimantas Gabrielius
from the table clienteHeitor Mourão

2 Answers

1
votes

You should just be able to change your @foreach loop. In a typical @foreach loop, you are looping through a group of records to display information about each singular record. This is why typically, you will see @foreach($records as $record).

I'm not sure what the singular version of 'cliente' is in your language, so I used client. Then, you are calling the nome, endereco, etc. from the individual client record. See if this @foreach helps:

@foreach($cliente as $client)
    <li>{{$client->nome}}</li>
    <li>{{$client->endereco}}</li>
    <li>{{$client->email}}</li>
    <li>{{$client->celular}}</li>
@endforeach
0
votes

First of all, if you are returning a group of elements, be consistent with the variables names that you use (use clientes instead of cliente):

class CadCliController extends Controller {

    public function cadCli() {
        $clientes = Cliente::all();

        return view('cadCli', ['clientes' => $clientes]);
    }
}

Now, in your view If you see in your @foreach statement, you are doing this:

...
@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
...

As you can see, those variables ($nome, $endereco, etc) doesn't exist because you are defining every item of the $clientes array as $cliente, so I thing those are attributes of each object:

...
@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
...