1
I have the following data in the controller:
$sheets = printerChange::select('folhas');
$sheetsNew = printerChange::select('folhasnew');
$sheetsCompare = (int)$sheetsNew;
if(!is_null($sheetsCompare > 0)){
$calcSheets->folhasnew = $request->folhas - $calcSheets->folhas;
}
else{
$calcSheets->folhasnew = $request->folhas;
}
When I run, the following error appears in my browser:
Object of class Illuminate Database Eloquent Builder could not be converted to int
I did a lot of research on the Internet and I couldn’t find anything that would help me, I saw several ways of conversion but none helped me up to this point.
It will be something wrong in my syntax?
PS: as a database I am using Postgresql.
Is that in the controller? could you post your model as well? which version of the Laravel is using?
– Alvaro Alves
Good morning Alvaro, my Model looks like this: <? php namespace App; use Illuminate Database Eloquent Model; class printerChange extends Model { protected $table = 'transicao'; protected $primaryKey = 'idtransicao'; public $timestamps = false; public $increment = false; } The version of the Laravel I am using is 5.7... I started learning PHP and Laravel very recently. Anyway, thank you so much.
– Gabriel Sassaki
another thing give a
var_dump
in the variable$sheetsNew
before trying to make the conversion– Alvaro Alves
You are trying to convert a Collection to int, it will never work, you should give a Count() in the $sheetsNew; $sheetsCompare = Count($sheetsNew);
– Kayo Bruno
Kayo, if I make $sheetsNew = printerChange::select('folhasnew')->first(); instead of $sheetsNew = printerChange::select('folhasnew'); ?
– Gabriel Sassaki
Already tried this: $sheetsCompare = (int) ($sheetsNew->get());
– fajuchem
Hi fajuchem, I tried the way you suggested but the error remains the same...?
– Gabriel Sassaki
Try to do so:
sheetsNew = printerChange::select('folhasnew')->count()
– adventistaam
Guys, thank you so much for the help, I don’t know if it’s right but I put the code inside a foreach and it worked, I mean, it worked in terms, the error doesn’t appear anymore but, my folhasnew field is still null... Anyway, I think this is already another question right right...
– Gabriel Sassaki
Or try it this way:
DB::table('folhasnew')->count();
– adventistaam
Thanks adventistaam, but if I do a Count();, won’t return me the folhasnew field values count? the count of times this field appears with data?
– Gabriel Sassaki
Guys, sorry it took me so long to come back here but I managed to solve my problem by changing my select, it was like this:$oldsheet = printerChange::Latest('sheets') ->Where('brand', $request->brand) ->Where('model', $request->model) ->Where('toner', $request->toner) ->Where('marca_toner', $request->tonerMarca) ->first();
– Gabriel Sassaki
The other variable: $pageCount = printerChange::Latest('folhasnew') ->Where('brand', $request->brand) ->Where('model', $request->model) ->Where('toner', $request->toner) ->Where('marca_toner', $request->tonerMarca) ->first();
– Gabriel Sassaki
O calculo em si:
if(is_null($pageCount)){
 $printerChange->folhasnew = 0;
 }
 elseif($pageCount->folhasnew >= 0){
 $printerChange->folhasnew = 0;
 $pageCount->folhasnew = $request->sheets - $oldsheet->sheets; }
– Gabriel Sassaki