wobsoriano
wobsoriano

Reputation: 13432

Laravel Eloquent Create

Hello so I've started using Laravel and it is useful and easy. For now I have a CRUD in which is working. In my AccountController@store the code is:

public function store(Request $request)
{
    $input = $request->all();
    Accounts::create($un);
    Session::flash('flash_message', 'Account successfully added!');
    return redirect()->route('accounts.index');
}

This basically adds a new account in my table. My problem is, I have a password textbox and I can't hash it since this code automatically gets every input in the form. How can I get it one by one? Like username, email and password only so I can hash the password.

Upvotes: 0

Views: 1571

Answers (3)

soote
soote

Reputation: 3260

You call Input::all() to get all the attributes passed in, and Input:get('key') to get a specific key.

So you should call:

$account = new Accounts;
$account->username = Input::get('username');
$account->password = Hash::make(Input::get('password'));

//key with a default
$account->password = Input::get('age', 20);

//optional field
if (Input::has('optional')) {
    $account->optional = Input::get('optional');
}

//any other fields that account needs

$account->save()

Upvotes: 0

Jirennor
Jirennor

Reputation: 1289

You could get the input one by one and then hash the password and save it to the database. But that would require extra code.

You could also add an extra function to your Account model that will take care of this automatically.

Take a look at the example I use to create my management users.

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

use Hash;

class Management extends Model implements AuthenticatableContract {

    use Authenticatable;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'Management';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * Automatic hash function for the password.
     *
     * @var array
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

}

Regarding your code, you could do this:

public function store(Request $request)
{
    Accounts::create($request->all());
    Session::flash('flash_message', 'Account successfully added!');
    return redirect()->route('accounts.index');
}

Make sure to modify the example model above to your own needs!

Upvotes: 3

Jake Opena
Jake Opena

Reputation: 1525

You can also do:

public function store(Request $request)
{
    $input = $request->all();

    Accounts::create([
        'username' => $input['username'],
        'password' => bcrypt($input['password']),
    ]);

    Session::flash('flash_message', 'Account successfully added!');
    return redirect()->route('accounts.index');
}

Upvotes: 1

Related Questions