$imoveis is Undefined, indefinite variable in Blade

Asked

Viewed 89 times

-1

The variable $imoveis in Blade is undefined, for me it is correct in the code.

@extends('layouts.main')

@section('title', 'Web Room')

@section('content')

<h1>Imoveis web</h1>

  <div id="search-container" class="col-md-12">
     <h1>Busque um imóvel</h1>
     <form action="">
         <input type="text" id="search" name="search" class="form-control" placeholder="Procure um imóvel..">
     </form>
  </div>
  <div id="imoveis-container" class="col-md-12">
      <h2>Próximos Imoveis</h2>
      <p>Veja os eventos dos proximos dias</p>
      <div id="cards-container" class="row">
      @foreach($imoveis as $imovel)
        <div class="card-col-md-3">
            <img src="/img/banner.jpg" alt="{{$imovel->titulo}}">
            <div class="card-body">
            <p class="card-date">10/09/2021</p>
            <h5 class="card-title">{{$imovel->titulo}}</h5>
            <p class="card-participantes">x participantes</p>
            <a href="#" class="btn btn-primary">Saber mais</a>
        </div>
  </div>
@endforeach
</div>
@endsection

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Imovel;

class ImoveisController extends Controller
{
    public function index(){

        $imoveis = Imovel::all();
        dd($imoveis);

        return view('welcome', ['imoveis' => $imoveis]);

    }

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

Model

<?php

namespace App\Models;

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

class Imovel extends Model
{
    use HasFactory;

    protected $table = "imoveis";

    protected $fillable = ['titulo','descricao','valor_aluguel','endereco'];
}
  • 1

    You made a dd($imoveis) in your Controller, then Blade will not run. If it is running yet, it is not from the method index. If it is from it, would be displayed the result of dd. Which of the two is?

  • Can you post the full error text? It may be that q is undefined one of the immovable->title fields for example

1 answer

-3

In function index() class ImoveisController try passing the properties to the view with the function compact() that way:

return view('welcome', compact('imoveis'));

The compact() makes a kind of structuring through the label of the variable and you can use in Blade with the same label name $imoveis:

example:

$names = ['Jon Snow', 'Aerys Targaryen']; 

$compacted = compact('names');

//saída de print_r($compacted); 

Array
(
    [names] => Array
        (
            [0] => Jon Snow
            [1] => Aerys Targaryen
        )

)
//saída de print_r($compacted['names']);
Array
(
    [0] => Jon Snow
    [1] => Aerys Targaryen
)
  • Ola continues with the same mistake friend.

  • try to perform the following commands on the project root in a terminal: php artisan config:clear, php artisan config:cache, php artisan cache:clear, php artisan view:clear; and return if it continues the same error I edited up there was a small error and syntax, can try again also the line above?

  • 1

    What’s the difference between using the compact in relation to the current code, view('welcome', ['imoveis' => $imoveis])?

  • Woss with the compact() Voce can pass several variables to Blade by compressing them into a single array, maybe it is not the solution to Ricardo’s error since he has only one, but it works the same way, but it’s how I pass variables to Lade in a more elegant way and with minimal impact if I need refactoring or add other variables in the future. I thought there was some mistake in that direction, just passing the variable to the front, but the way he’s doing it really n is so relevant

  • @emmanuelpires gave the commands but unfortunately the error remains.

  • @Woss I know Compact is a little slower but nothing that will stop the application. Particularly use a lot because aesthetically it gets more "beautiful" and easy to read in the IDE (opinion). In practice >> Return view('Welcome', ['data' => $data, 'title' => $title, 'filter' => $filter,'user' =>$user,'outroX' => $outroX, 'outroY' => $outroY,'outroX' => $outroZ ]); //Versus Compact Return view('Welcome', Compact('data', 'title' , 'filter' ,'user' ,'outroX' ,'outroY' ,'outroX'); These 2 posts in the English OS have had a reasonable discussion regarding the use of the explicit variable Compact >

  • https://stackoverflow.com/questions/16319729/why-does-php-compact-use-strings-instead-of-actual-variables and https://stackoverflow.com/questions/35589085/compact-vs-manual-array-declaration-on-php

  • @Marcosxavier Yes, I have science, but I am grateful for the links, it will probably help several people. I even wondered why the answer was given as if the compact solved the question problem, while it would have exactly the same effect as passing the array directly, as the author of the question.

Show 3 more comments

Browser other questions tagged

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