Laravel配置文件编辑

时间:2016-01-08 09:08:15

标签: laravel edit profile

好吧,我已经用这种方式来保存用户信息并且工作正常,

 static public function memberSave($request) {

        $signup = false;

        $member = new Members();
        $member->name = $request['name'];
        $member->email = $request['email'];
        $member->password = bcrypt($request['password']);
        $member->save();

        if (!empty($member->id)) {

            $new_id = $member->id;
            DB::insert("INSERT INTO roles VALUES ($new_id, 5613)");
            $signup = true;
            Session::flash('sm', 'Thank you! You have signed up successfully!');
        }

        return $signup;
    }

但是当用于编辑个人资料(由用户)时,它不起作用 因为我使用new(); (制作对象)

我也没有成功使用find();所以我试着用这个

static public function saveProfile($id,$name,$email,$password) {


        $sql = "UPDATE members SET name=?,email=?,password=? WHERE id=?";
        $member = DB::select($sql, [$name,$email,$password,$id]); 

但是当我想在laravel中加密密码时无效。

这也是第二页中的代码

public function postProfile(ProfileValidation $request) {
    if (Members::saveProfile($request['id'], $request['name'], $request['email'], $request['password'])) {

        return redirect('');
    }
}

我希望通过laravel帮助编辑用户个人资料,谢谢。

2 个答案:

答案 0 :(得分:0)

您的Members课程必须延长Eloquent\Model才能使此代码生效。

class Members extends Model {
    // optional
    protected $table = 'members';
...

使用电子邮件查找和更新会员

// find the single member
$member = Members::where('email', request['email'])->first();
// update the member
$member->name = $request['name'];
$member->password = $request['password'];
// now save the updated member
$member->save();

为了加密密码,Laravel提供Hash Facade,

// import this
use Hash;
...
// encrypt Password
$encrypted = Hash::make($request['password']);
...

答案 1 :(得分:0)

如果您希望用户在您的模型中自动散列密码:

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = Hash::make($value);
}

您可以直接检查用户是否存在create new或更新它:

public function saveMember($request)
{
    $member = Member::findOrNew($request->email);
    //All your input you want to save
    $member->save();
}