Laravel, foreach and database

1

I have the following table in the database

  public function up()
{
     Schema::create('cidades', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('uf');
        $table->string('nome_uf');
        $table->integer('mesorregiao_geografica');
        $table->string('nome_nesorregiao');
        $table->integer('microrregiao_geografica');
        $table->string('nome_microrregiao');
        $table->integer('municipio');
        $table->integer('cod_municipio_completo');
        $table->string('nome_municipio');
    });
}

I've already created the city model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cidade extends Model
{
    //
}

I have already tried to insert data from tinker and it worked.

use App\Cidade
$cidade= new cidade
$cidade->nome_uf= "teste";
$franqueado->save();

But now I can not give a foreach, in the view .... How do I list the names of all cities and give a foreach to appear in the view?

    
asked by anonymous 20.06.2016 / 19:52

2 answers

2

First I advise you to insert into your Model the values that will be editable with the fillable parameter.

Model (in the folder of your project \ app \ Cidade.php)

<?php namespace Cidades;

use Illuminate\Database\Eloquent\Model;

class Cidade extends Model {

    //------------Dados Editavéis----------------------
    //Definindo dados que poderão ser alterados
    protected $fillable = [
        'uf',
        'nome_uf',
        'mesorregiao_geografica',
        'nome_nesorregiao',
        'microrregiao_geografica',
        'nome_microrregiao',
        'municipio',
        'cod_municipio_completo',
        'nome_municipio'
    ];

}

Controller CityController.php

...
public function index()
{
    $cidades = \Cidades\Cidade::orderBy('id');
    return view('cidade.index', compact('cidades'));
}
...

View index.blade.php in your project in the \ resources \ views folder

...
<tr>
    <th> ID </th>
    <th> UF </th>
    <th> NOME UF </th>
    <th> ... </th>
</tr>
@if($cidades)
    @foreach($cidades as $cidade)
        <tr>
            <td>{{$cidade->id}}</td>
            <td>{{$cidade->uf}}</td>
            <td>{{$cidade->nome_uf}}</td>
            <td>{{$cidade->...}}</td>
        </tr>
    @endforeach
@endif
...

No error

    
21.06.2016 / 00:13
1

Considering that your ORM is working, I'll show you a simple way to solve the problem, sharing data between a controller method and its view works as follows:

app \ Http \ Controllers \ UserController.php

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();

        return view('user.index', compact('users'));
    }
}

As I'm looking for the view 'user.index' in the controller, we'll need the file in the exact location:

resources / views / user / index.blade.php

...

<ul>
@foreach ($users as $user)
    <li>
        {{ $user->name }}
    </li>
@endforeach
</ul>

..

If you are struggling in this matter, I suggest a thorough reading of the ORM-related topics in the official Laravel , is in English but it is a simple text and easy to read.

    
20.06.2016 / 20:07