Consultation in laravel

1

I make this query:

$costo = Habitacion::where("id","=", $habestatus)->select('hab_costo')->first();

* Habestatus is a variable that has the id of the room.

and when you print the result, you throw this at me:

{"hab_costo":2000}

I just want the number as a result.

    
asked by Estefania 05.03.2018 в 23:28
source

1 answer

1

If you only want the cost of hab_costo, you can do several things:

  • Get the property directly:

    $costo = Habitacion::whereId($habestatus)
                ->select('hab_costo')
                ->first()
                ->hab_costo;
    
  • Not so specific but pluck can also serve:

    $costo = Habitacion::whereId($habestatus)
                ->select('hab_costo')
                ->first()
                ->pluck('hab_costo');
    

Keep in mind that you can reduce some code by using whereVariable()

    
answered by 05.03.2018 / 23:32
source