Integer conversion in Laravel Controller

Asked

Viewed 70 times

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?

  • 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.

  • another thing give a var_dump in the variable $sheetsNew before trying to make the conversion

  • 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, if I make $sheetsNew = printerChange::select('folhasnew')->first(); instead of $sheetsNew = printerChange::select('folhasnew'); ?

  • Already tried this: $sheetsCompare = (int) ($sheetsNew->get());

  • Hi fajuchem, I tried the way you suggested but the error remains the same...?

  • Try to do so: sheetsNew = printerChange::select('folhasnew')->count()

  • 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...

  • Or try it this way: DB::table('folhasnew')->count();

  • 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?

  • 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();

  • 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();

  • O calculo em si:&#xA;if(is_null($pageCount)){&#xA; $printerChange->folhasnew = 0;&#xA; }&#xA; elseif($pageCount->folhasnew >= 0){&#xA; $printerChange->folhasnew = 0;&#xA; $pageCount->folhasnew = $request->sheets - $oldsheet->sheets; }

Show 9 more comments
No answers

Browser other questions tagged

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