Something wrong with if in the controller Laravel

1

I have a function in a laravel controller, at first it seems to be working fine but when it goes through the "if" part it jumps straight to the "else", here is the code that is in the controller:

//Inserção de troca(transicao) e decremento na tabela toner
public function printerInsert(Request $request){

    $oldsheet = printerChange::latest('folhas')
        ->where('marca', $request->marca)
        ->where('modelo', $request->modelo)
        ->where('toner', $request->toner)
        ->where('marca_toner', $request->tonerMarca)
        ->first();            

    $pageCount = printerChange::latest('folhasnew')
        ->where('marca', $request->marca)
        ->where('modelo', $request->modelo)
        ->where('toner', $request->toner)
        ->where('marca_toner', $request->tonerMarca)
        ->first();          

    $printerChange = new printerChange;
    $printerChange->setor = $request->setor;
    $printerChange->marca = $request->marca;
    $printerChange->modelo = $request->modelo;
    $printerChange->toner = $request->toner;
    $printerChange->marca_toner = $request->tonerMarca;
    $printerChange->data = $request->dataTroca;
    $printerChange->folhas = $request->folhas;            

    if($pageCount->folhasnew > 0){
        $printerChange->folhasnew = $request->folhas - $oldsheet;
    }
    else{
        $printerChange->folhasnew = $request->folhas;
    }

    dd($printerChange);

    //Decremento toner
    $printerSub = printer::where('modelo', $request->toner)
        ->where('marca', $request->tonerMarca)
        ->first();

    $printerSub->quantidade = $printerSub->quantidade - $request->quantidade;
    $printerSub->data = $request->dataTroca;

    //Save nas tabelas
    $printerSub->save();
    $printerChange->save();

    return redirect()->to('/printer');
}

I must have done something wrong, I just could not figure out what yet, can anyone help me?

A is, the DB is postgres, I do not know if it influences more ...

Here is the information that results from dd ($ pageCount);:

printerChange {#223 ▼
  #table: "transicao"
  #primaryKey: "idtransicao"
  +timestamps: false
  +increment: false
  #connection: "pgsql"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:9 [▼
    "idtransicao" => 14
    "data" => "2018-12-06"
    "setor" => "Administrativo"
    "marca" => "Brother"
    "modelo" => "DCP-J125"
    "toner" => "PRETO"
    "marca_toner" => "J.PROLAB"
    "folhas" => 456654
    "folhasnew" => null
  ]
  #original: array:9 [▼
    "idtransicao" => 14
    "data" => "2018-12-06"
    "setor" => "Administrativo"
    "marca" => "Brother"
    "modelo" => "DCP-J125"
    "toner" => "PRETO"
    "marca_toner" => "J.PROLAB"
    "folhas" => 456654
    "folhasnew" => null
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}

Thanks a lot already.

    
asked by anonymous 06.12.2018 / 18:41

1 answer

1

Just change your if: from

 if($pageCount->folhasnew > 0){
    $printerChange->folhasnew = $request->folhas - $oldsheet;
}else{
    $printerChange->folhasnew = $request->folhas;
}

for

if(is_null($pageCount->folhasnew)){ 
    $printerChange->folhasnew = $request->folhas - $oldsheet->folhas; 
}else{ 
    $printerChange->folhas = $request->folhas; 
}
    
06.12.2018 / 20:13