Home > OS >  why am i getting undeclared variable error in blade (in laravel)?
why am i getting undeclared variable error in blade (in laravel)?

Time:11-15

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 App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Aluno;

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 App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Aluno extends Model
{
    use HasFactory;
}

migation

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

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

CodePudding user response:

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

CodePudding user response:

TANKS everyone for help me; I find where the problem was, I just forget to config my routes, so they were past the variable to the blade

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

  • Related