Object of class IlluminateSupportCollection 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 IlluminateSupportCollection 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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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') }}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x