Reduced code for insert

1

Let's say you have the following fields in the users table:

  

ID , NOME , USUARIO , LOGIN and SENHA

For me to insert a record into the table I do the following

$usuario = new Usuario();
$usuario->nome = 'Carlos Bruno';
$usuario->login = 'cbcarlos';
$usuario->senha = '123456';
$usuario->save();

There is a shorter code for me to do this insert, because if it were too many fields?

Example :

$usuario = new Usuario( AllInput() )
$usuario->save()

Type if I wanted to get from an array

Array(
 'Carlos Bruno',
 'cbcarlos',
 '123456' 
);

And insert in the base, would like?

    
asked by anonymous 23.05.2018 / 22:24

1 answer

2

Yes, there is a way to pass% associative on for example:

$newarray = array(
 'nome' => 'Carlos Bruno',
 'login' => 'cbcarlos',
 'senha' => '123456' 
);

The% d of only one dimension will not work, because internally the value is passed by the name of the configured key in array , example:

class Usuario extends Eloquent 
{
    protected $fillable = array('nome', 'login', 'senha');
}

and to save can be done in two ways:

$usuario = new Usuario($newarray);
$usuario->save();

or

$usuario = Usuario::create($newarray);

If the values are coming from array and a fillable request, for example this works without problems.

    
23.05.2018 / 22:42