Something wrong with if in Laravel controller

Asked

Viewed 83 times

1

I have a function in a controller of the Laravel, at first it seems to be working well but, when it goes through the part of "if" 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 sure must have done something wrong, I just couldn’t figure out what yet, someone can give me a hand?

A is, the comic is postgres, I don’t know if it influences but...

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 => "*"
  ]
}

Thank you so much.

1 answer

1


just you change your if: of

 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; 
}

Browser other questions tagged

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