I can’t get into the foreach of a Lade at Laravel 5.7

Asked

Viewed 58 times

2

I’ve got a little problem I haven’t figured out yet, I’ve got a View and I can’t get into foreach of the same, follows the View:

<div class="form-group">
    {!! csrf_field() !!}        

    <!--{{ dd($impressora) }}-->
    @foreach($impressora as $sheets)
        {{ dd('teste') }}
        <!--{{ dd($sheets) }}-->
        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="setor">Setor*:</label>
                <input class="form-control" placeholder="Digite o setor..." required="required" name="setor" type="text" value="{{ $sheets->setor }}" id="setor">
            </div>
            <div class="form-group col-md-4">
                <label for="marca">Marca*:</label>
                <input class="form-control" placeholder="Digite a marca..." required="required" name="marca" type="text" value="{{ $sheets->marca }}" id="marca">
            </div>
            <div class="form-group col-md-4">
                <label for="modelo">Modelo*:</label>
                <input class="form-control" placeholder="Digite o modelo..." required="required" name="modelo" type="text" value="{{ $sheets->modelo }}" id="modelo">
            </div>     
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="tipo">Tipo*:</label>
                <input class="form-control" placeholder="Digite o tipo do tonner..." required="required" name="tipo" type="text" value="Tinta" id="tipo">
            </div>
            <div class="form-group col-md-4">
                <label for="tonner">Tonner*:</label>
                <input class="form-control" placeholder="Digite o modelo do tonner..." required="required" name="tonner" type="text" value="Tinta" id="tonner">
            </div>
            <div class="form-group col-md-4">
                <label for="setor">Setor*:</label>
                <input class="form-control" placeholder="Digite o setor..." required="required" name="setor" type="text" value="T.I." id="setor">
            </div>
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="local">Local*:</label>
                <select class="form-control" required="required" id="local" name="local">
                    <option value="MG0" selected="selected">Fábrica Minas</option>
                    <option value="SP0">Loja SP Mendes Júnior</option>
                    <option value="SP1">Loja SP Xavantes</option>
                </select>
            </div>
            <div class="form-group col-md-4">
                <label for="datacompra">Data da Compra*:</label>
                <input class="form-control" required="required" name="datacompra" type="date" value="2015-08-05" id="datacompra">
            </div>
            <div class="form-group col-md-4">
                <label for="ativo">Em Uso*:</label>
                <select class="form-control" required="required" id="ativo" name="ativo">
                    <option value="S" selected="selected">Sim</option>
                    <option      value="N">Não</option>
                    <option value="0">Descartado</option>
                </select>
            </div>
        </div>
    @endforeach
</div>

This is the controller:

public function tonerSheets($id)
{        
    $impressora  = $this->TiImpressoraM->find($id);                
    return view('portal.ti.impressoras.TiImpressorasToner', compact('impressora'));
}

Before the foreach everything goes normal, even in the test done but does not enter the foreach for nothing in this world, what did I do wrong?

PS: I searched in a lot of places including here on the site and could not find anything in that sense, I saw many problems of foreach but none similar to this, if there is already something with the same idea that can help could please direct me?

Dump result before the foreach:

Tiprinter {#898 #fillable: array:9 [ 0 => "asset" 1 => "ip" 2 => "tag" 3 => "model" 4 => "tonner" 5 => "sector" 6 => "date purchase" 7 => "local" 8 => "active" ]] #Connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #Attributes: array:12 [ "id" => 1 "patrimonio" => 35 "ip" => "" "tag" => "Brohter" "model" => "DCP-J125" "tonner" => "Ink" "sector" => "T.I." "datacompra" => "2015-08-05" "local" => "MG0" "active" => "S" "created_at" => null "updated_at" => "2018-04-13 19:14:06" ]] #original: array:12 [ "id" => 1 "patrimonio" => 35 "ip" => "" "tag" => "Brohter" "model" => "DCP-J125" "tonner" => "Ink" "sector" => "T.I." "datacompra" => "2015-08-05" "local" => "MG0" "active" => "S" "created_at" => null "updated_at" => "2018-04-13 19:14:06" ]] #Casts: [] #Dates: [] #dateFormat: null #appends: [] #Events: [] #observables: [] #Relations: [] #touches: [] +timestamps: true #Hidden: [] #Visible: [] #guarded: array:1 [ 0 => "*" ] }

I don’t know if this is how I should format...

  • $printer is not a list! So it doesn’t work

  • Hi @Virgilio Novic, thanks for answering, but I still got in doubt, by list you mean array right? If this is the case, in the test I did, out of the foreach it returns me the array as it should but, inside the foreach not, by the way, nor enter?

  • $impressora = $this->TiImpressoraM->find($id); return a printer or null, ai I was in doubt you want to return only one or a list of printers?

  • Return me the array with the information of a specific printer, as I expected.

  • So this command returns a printer so this is what you want to test if the printer returned values in object or array???

  • What I need is the same array, inside the foreach, I need some volores inside the array to pass them to the view and eventually also use these values for another operation.

  • $impressora = $this->TiImpressoraM->all() this return you the collection (or array) the other way a printer! which of the two ...?

  • Sorry @Virgilio Novic, I don’t understand the question...

  • Let’s try to understand: the command you gave in controller returns an instance of that Tiprinted class, then you went to View is want to print the collection of this class, this will not work because $printer is a printer and is not a printer collection. If the command is right on controller the foreach is wrong in View? I already asked you if it is a printer the result even if it is I tell you what to do!

  • Sorry @Virgilio Novic, you may have noticed that I am very new in PHP and Laravel, anyway, I posted the result of the Dump before the foreach in the original question, if it helps...

  • No problems you want a result just look at the answer

  • A question, why are you trying to iterate a Model and are still using the name $sheets? Wouldn’t you want to pick up a relationship from that Model? Type $impressora->sheets?

Show 7 more comments

1 answer

1


You are not returning a collection, you are returning a simple dice, remove the foreach and put a if, example:

<div class="form-group">
    {!! csrf_field() !!}        
    @if(isset($impressora))            
        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="setor">Setor*:</label>
                <input class="form-control" placeholder="Digite o setor..." required="required" name="setor" type="text" value="{{ $sheets->setor }}" id="setor">
            </div>
            <div class="form-group col-md-4">
                <label for="marca">Marca*:</label>
                <input class="form-control" placeholder="Digite a marca..." required="required" name="marca" type="text" value="{{ $sheets->marca }}" id="marca">
            </div>
            <div class="form-group col-md-4">
                <label for="modelo">Modelo*:</label>
                <input class="form-control" placeholder="Digite o modelo..." required="required" name="modelo" type="text" value="{{ $sheets->modelo }}" id="modelo">
            </div>     
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="tipo">Tipo*:</label>
                <input class="form-control" placeholder="Digite o tipo do tonner..." required="required" name="tipo" type="text" value="Tinta" id="tipo">
            </div>
            <div class="form-group col-md-4">
                <label for="tonner">Tonner*:</label>
                <input class="form-control" placeholder="Digite o modelo do tonner..." required="required" name="tonner" type="text" value="Tinta" id="tonner">
            </div>
            <div class="form-group col-md-4">
                <label for="setor">Setor*:</label>
                <input class="form-control" placeholder="Digite o setor..." required="required" name="setor" type="text" value="T.I." id="setor">
            </div>
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="local">Local*:</label>
                <select class="form-control" required="required" id="local" name="local">
                    <option value="MG0" selected="selected">Fábrica Minas</option>
                    <option value="SP0">Loja SP Mendes Júnior</option>
                    <option value="SP1">Loja SP Xavantes</option>
                </select>
            </div>
            <div class="form-group col-md-4">
                <label for="datacompra">Data da Compra*:</label>
                <input class="form-control" required="required" name="datacompra" type="date" value="2015-08-05" id="datacompra">
            </div>
            <div class="form-group col-md-4">
                <label for="ativo">Em Uso*:</label>
                <select class="form-control" required="required" id="ativo" name="ativo">
                    <option value="S" selected="selected">Sim</option>
                    <option   value="N">Não</option>
                    <option value="0">Descartado</option>
                </select>
            </div>
        </div>
    @end
</div>
  • I tried to change the foreach for if, but continues the same way, does not enter the block of if...

  • @Gabrielsassaki boy is to work!

  • Weird huh, I gave a Dump with the remote {{ dd($impressora) }} before the if and after the if, the one before it worked, the one after it didn’t...

  • Down there is a @endforeach, put only @end

  • @Virgilionovic missed a change $sheets for $impressora in the HTML within the if. I guess that’s why your example didn’t work for him.

  • I edited the reply again @Gabrielsassaki

  • I edited the reply @fernandosavio is very html we end up getting lost

  • I switched yes, @endforeach for @endif and changed everything you need inside the block if...

  • I commented because that may be why you solution did not work for him.. If he only copied and put will give error

  • @Gabrielsassaki did not work?

  • I get it, it didn’t work, it happened the same as foreach...

  • I think you’re missing @Gabrielsassaki only can, because it’s supposed to work!

  • Really, it’s supposed to work but for some reason it’s not going, tomorrow I’m going to review everything possible and I warn you, suddenly it’s something really stupid... But you still see...

Show 8 more comments

Browser other questions tagged

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