Why is it saved with brackets in my bd when I check the text of a combobox in laravel?

0

I try to get the text of a combobox from the controller so I make a query to the bd with the value of the combobox obtained but when I save it it does it with the following format ["Plant I"] (I do not know why it keeps me with bracket and quotes I only want the text). The driver code is as follows

$idpiso=$request->get('pisoroom');
      $nombrepiso=DB::table('levels')->where('id',$idpiso)
      ->pluck('nombre');
$rooms=Room::create([
         'ubicacion'=>$nombrepiso
        ]);
    
asked by lucho 18.12.2017 в 13:29
source

1 answer

3

The problem is given because the where function returns an array so it saves it with the [value] format since you indicate that you are going to leave all the fields only the name, when you call pluck ('name')

If you do the following you will be able to realize what I am saying:

$nombrepiso = (string)DB::table('levels')->where('id',$piso)->pluck('nombre');
dd($nombrepiso);

So for you to get what you want you only have to call the first function since it means you only want 1 and if you search for the id, this is almost always primary key you will only find 1. So when doing this:

$idpiso=$request->get('pisoroom');
$nombrepiso = (string)DB::table('levels')->where('id',$piso)->pluck('name')->first();
$rooms=Room::create([ 'ubicacion'=>$nombrepiso ]);

You can now save the name as you expected. I hope you solve this.

    
answered by 18.12.2017 / 14:03
source