Francesco
Francesco

Reputation: 83

Object of class Illuminate\Support\Collection could not be converted to int Laravel 8

I am having troubles with retrieving an integer value from Query builder of Laravel. It gives me this error as the title of the question:

Object of class Illuminate\Support\Collection could not be converted to int

i tried to use also the pluck() method but with the same error.

Here is the code portion of code:

 Scegli Corso: <select name="tipo" id="selezionaCorsoIscrizione">
                       {{ $corsi_scii = DB::table('corsoscii')->select('idCorso','nome')->get() }}
                       {{ $post_corso = DB::table('corsoscii')->select('membriMax')->pluck('membriMax') }}
                                    
                       <option value="" selected="selected"> Seleziona Corso
                       </option>
                        @foreach($corsi_scii as $corso_scii)
                        <option value=""> 
                        @if($post_corso == 0)
                           {{ "Il corso ha raggiunto la capienza massima" }}
                        @else
                            {{ $corso_scii->idCorso." - ".$corso_scii->nome }}
                        @endif
                        </option>
                        @endforeach

Upvotes: 2

Views: 608

Answers (1)

N69S
N69S

Reputation: 17206

pluck() returns a collection of the field you selected. You can't compare it to 0. Use value() instead, it returns the first available value or null.

Replace this line

{{ $post_corso = DB::table('corsoscii')->select('membriMax')->pluck('membriMax') }}

with

{{ $post_corso = DB::table('corsoscii')->select('membriMax')->value('membriMax') }}

Upvotes: 4

Related Questions