skip to Main Content

I am new in laravel and I try to make a it display on screen some information form my migration but it keeps giving me an error of undeclared variable in the "alunos" blade

this is my controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsAluno;

class AlunoCoontroller extends Controller
{
    public function index()
    {
        $alunos = Aluno::all();

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

    public function create()
    {
        return view('alunos.create');
    }

}

Model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Aluno extends Model
{
    use HasFactory;
}

migation

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('_alunos', function (Blueprint $table) {
            $table->id();
            $table->string('nome');
            $table->string('filme');
            $table->string('RA');
            $table->string('senha');
            $table->string('cpf');
            $table->string('cep');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * 
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('_alunos');
    }
};

and the blade

@extends('layouts.main')
 
@section('title', 'Star-Fox Company')

@section('content')
    <h1>tela onde o aluno vai inserri os dados para acessar seu cadastro</h1>

    @foreach ($alunos as $aluno)
        <p>Nome: {{ $aluno->nome }}</p>
        <p>Matricula: {{ $aluno->filme }}</p>
        <p>Curso: {{ $aluno->curso }}</p>
        <p>Senha: {{ $aluno->senha }}</p>
        
    @endforeach

@endsection

I already try change the name of this variable but the error keeps, can someone help me with this
explaining why it giving an undeclared variable error on the blade

2

Answers


  1. Chosen as BEST ANSWER

    I find where the problem was. I just forgot to config my routes, so they were past the variable to the blade file:

    Route::get('/area_do_aluno/{id}', [AlunoCoontroller::class, 'index'] );
    

  2. You are missing $aluno->curso in your migration.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search