Laravel, foreach and database

Asked

Viewed 1,613 times

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
{
    //
}

already tested insert data from Tinker and worked.

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

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

2 answers

2


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

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

<?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 Cidadecontroller.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 mistake

  • In fact if you’re gonna put all the fields like $fillable you might as well use protected $guarded = [];, https://laravel.com/docs/5.4/eloquent#mass-assignment

1

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

Http Controllers app Usercontroller.php

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

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

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

Resources/views/user/index.blade.php

...

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

..

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

  • Class 'App Http Controllers User' not found Gave this error

  • You should not use the User class, but the City class. I used User just so you understand the process of sharing information between the controller and the view. It is very important that you follow a tutorial before trying to make your own projects, such as this for example, the official documentation of Laravel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.