I cannot use the foreach with object twice in the same view in the Laravel

Asked

Viewed 501 times

1

I have a big problem that I can’t solve because I’m still a beginner in Laravel. The problem is I’m developing a web application and I need to list the cities object in two different select, but when I add the second foreach gives an error saying that I cannot find the object but when I delete this foreach and leave only one the application works normally. Follow the code part where I need to make the two listings:

    <!-- Modal content-->
    <div class="modal-content">
        <div class="modal-body">
            <div class="row">
                <div class="col-md-12">
                    <img src="/img/banner.png" class="img-responsive">
                </div>
                {!! Form::open(['url'=>'index/oportunidades']) !!}
                <div class="col-md-12">

                        <div class="form-group">
                            {!! Form::label('cidades', 'Selecione uma Cidade:') !!}
                            <select class="form-control" id="sel1" name="cidade">
                                @foreach($cidades as $cidades)
                                    <option value="{{$cidades->id}}">{{ $cidades->nome }} - {{ $cidades->estado->sigla }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            {!! Form::label('categorias', 'Selecione uma Categoria:') !!}
                            <select class="form-control" id="sel1" name="categoria">
                               @foreach($categorias as $categorias)
                                    <option value="{{$categorias->id}}">{{$categorias->nome}}</option>
                               @endforeach
                            </select>
                        </div>
                </div>
                <div class="col-md-12">
                    <button class="btn btn-warning" type="submit">Filtrar</button>
                </div>
                {!! Form::close() !!}

            </div>
        </div>

    </div>

    </div>
</div>

<!-- Modal Empresas -->
<div id="ModalEmpresas" class="modal fade" role="dialog">
    <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
        <div class="modal-body">
            <div class="row">
                <div class="col-md-12">
                    <img src="/img/banner.png" class="img-responsive">
                </div>
                {!! Form::open(['url'=>'index/oportunidades']) !!}
                <div class="col-md-12">

                        <div class="form-group">
                            {!! Form::label('cidades', 'Selecione uma Cidade:') !!}
                            <select class="form-control" id="sel1" name="cidade">
                                @foreach($cidades as $cidades)
                                    <option value="{{$cidades->id}}">{{ $cidades->nome }} - {{ $cidades->estado->sigla }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            {!! Form::label('categorias', 'Selecione uma Categoria:') !!}
                            <select class="form-control" id="sel1" name="categoria">
                               @foreach($categorias as $categorias)
                                    <option value="{{$categorias->id}}">{{$categorias->nome}}</option>
                               @endforeach
                            </select>
                        </div>
                </div>
                <div class="col-md-12">
                    <button class="btn btn-warning" type="submit">Filtrar</button>
                </div>
                {!! Form::close() !!}

            </div>
        </div>

    </div>

    </div>
</div>

And below the controller code:

public function index(){
    $cidades = cidades::all();
    $categorias = categorias::all();
    $oportunidades = oportunidades::all();
    return view('index', compact('cidades', 'categorias', 'oportunidades'));
}
  • In your example do not have the two city foreach you quoted. You can post the error as well?

  • Has face! check again. One is in the first modal and the second is in the second modal. Just like the foreach of the category that also gives error when running the application.

  • This is the error that gives: "Trying to get Property of non-object".

2 answers

3

The error:

@foreach($cidades as $cidades)
    <option value="{{$cidades->id}}">
          {{ $cidades->nome }} - {{ $cidades->estado->sigla }}
    </option>
@endforeach

Explanation: in foreach in the collection of cities being passed to the same variable, this overwriting cannot be a concept error, I change the variable name:

Correct:

@foreach($cidades as $c)
    <option value="{{$c->id}}">
          {{ $c->nome }} - {{ $c->estado->sigla }}
    </option>
@endforeach

Same case happens in Categories, arrange variable name, can’t be the same collection name.

Another optimization is in your controller in the model cities force the loading of state, This improves performance because it generates in 2 SQL what you need, while the other to each interaction is made a query in the bank (low performance):

public function index()
{
    $cidades = cidades::with('estado')->get();  
    $categorias = categorias::all();
    $oportunidades = oportunidades::all();
    return view('index', compact('cidades', 'categorias', 'oportunidades'));
}
  • Man I did what you said But the mistake still continues.

  • @Daislan what is the mistake? Post in your question the 3 models cities, categories and opportunities and also put what is or are the lines of the problem

  • It’s the same error as the question. It goes on to say that "Trying to get Property of non-object".

  • On which line @Daislan and put in your model what I asked for if it is not impossible to catch the error.

0

I finally solved the problem! It is because in the first foreach I assigned the variable $cities to my object $cities so in the second foreach it was catching variable $cities and not object $cities. That is, it was just a repeating error of variable.

Browser other questions tagged

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