Laravel 拉威尔雄辩的创造

Laravel 拉威尔雄辩的创造,laravel,Laravel,您好,我已经开始使用Laravel,它很有用,也很简单。现在我有一个积垢在里面工作。在我的AccountController@store代码是: public function store(Request $request) { $input = $request->all(); Accounts::create($un); Session::flash('flash_message', 'Account successfully added!'); ret

您好,我已经开始使用Laravel,它很有用,也很简单。现在我有一个积垢在里面工作。在我的AccountController@store代码是:

public function store(Request $request)
{
    $input = $request->all();
    Accounts::create($un);
    Session::flash('flash_message', 'Account successfully added!');
    return redirect()->route('accounts.index');
}
这基本上会在我的表中添加一个新帐户。我的问题是,我有一个密码文本框,我不能散列它,因为这个代码会自动获取表单中的每个输入。我怎样才能一个接一个地得到它?像用户名、电子邮件和密码一样,这样我才能散列密码。

您可以调用
Input::all()
来获取传入的所有属性,并调用
Input:get('key')
来获取特定的密钥

所以你应该打电话:

$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()
调用
Input::all()
获取传入的所有属性,调用
Input:get('key')
获取特定的键

所以你应该打电话:

$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()
您还可以执行以下操作:

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');
}
您还可以执行以下操作:

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

您可以逐个获取输入,然后散列密码并将其保存到数据库中。但这需要额外的代码

您还可以向您的帐户模型添加一个额外的功能,该功能将自动处理此问题

看看我用来创建管理用户的示例

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

}

确保根据您自己的需要修改上述示例模型

您可以逐个获取输入,然后散列密码并将其保存到数据库中。但这需要额外的代码

您还可以向您的帐户模型添加一个额外的功能,该功能将自动处理此问题

看看我用来创建管理用户的示例

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

}

确保根据您自己的需要修改上述示例模型

{!!Form::open(array('route'=>'accounts.store'))!!}是一篇文章。将立即尝试您的代码从何处获得新帐户()?帐户是什么类型的对象?我认为这是一个雄辩的模型。是的,这是一个雄辩的模型。应该是新账户();在你的帖子中是的,我已经改正了。在文档中,他们没有放置(),所以我也删除了它们。{!!Form::open(array('route'=>'accounts.store'))!!}是的,一篇帖子。将立即尝试您的代码从何处获得新帐户()?帐户是什么类型的对象?我认为这是一个雄辩的模型。是的,这是一个雄辩的模型。应该是新账户();在你的帖子中是的,我已经改正了。在文档中,他们不放置()项,所以我也放弃了这些。我尝试了这一次,但出现了此错误,无法将Authenticatable用作Authenticatable,因为该名称已在使用中。等等,我犯了一个错误,我想祝你的应用程序的其余部分好运。我尝试了这一次,但出现此错误,无法将Authenticatable用作Authenticatable,因为该名称已在使用中。等等,我犯了一个错误错误我认为祝你的应用程序的其余部分好运。在创建用户之前不要忘记进行一些验证。在创建用户之前不要忘记进行一些验证。