【问题标题】:Laravel Eloquent CreateLaravel Eloquent 创建
【发布时间】:2015-06-10 14:54:51
【问题描述】:

您好,我已经开始使用 Laravel,它既实用又简单。现在我有一个正在工作的 CRUD。在我的 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');
}

这基本上在我的表中添加了一个新帐户。我的问题是,我有一个密码文本框,但我无法对其进行哈希处理,因为此代码会自动获取表单中的每个输入。怎么才能一一拿到?像用户名、电子邮件和密码一样,我可以散列密码。

【问题讨论】:

  • 在创建用户之前不要忘记做一些验证。

标签: laravel


【解决方案1】:

您可以一一获取输入,然后对密码进行哈希处理并将其保存到数据库中。但这需要额外的代码。

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

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

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

}

关于您的代码,您可以这样做:

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

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

【讨论】:

  • 我试过这个并得到这个错误 Cannot use Authenticatable as Authenticatable because the name is already in use
  • 等等,我认为我犯了一个错误
  • 祝您应用程序的其余部分好运。
【解决方案2】:

你也可以这样做:

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

【讨论】:

    【解决方案3】:

    您调用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()
    

    【讨论】:

    • {!! Form::open(array('route' => 'accounts.store')) !!} 是一个 POST。现在将尝试您的代码
    • 你从哪里得到 new Account() ?
    • Accounts 是什么类型的对象?我认为这是一个 Eloquent 模型。
    • 是的,它是雄辩的。应该是 new Accounts();在你的帖子中
    • 是的小错字,我已经更正了。在文档中,他们没有放置 (),所以我也删除了这些。
    猜你喜欢
    • 2016-11-04
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2018-07-18
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多