如何在Laravel中创建用户帐户设置?

时间:2018-08-29 07:08:06

标签: php laravel authentication settings

我是Laravel和php的新手。我已经在laravel应用中成功创建了身份验证。现在,用户可以注册并登录其帐户。现在,我想在我的导航栏中创建一个帐户设置选项,该选项会将用户重定向到一个页面,在该页面上他们可以更改其名称,电子邮件和密码。谁能帮我解决这个问题?

这是我在数据库中的Users表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('provider')->nullable();
            $table->string('provider_id')->nullable();
            $table->string('google_id')->nullable();
            $table->rememberToken()->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

1 个答案:

答案 0 :(得分:-1)

您必须实现这样的用户个人资料控制器

例如:

class UsersController extends Controller
{
    //
    public function getList($message = null){
        $users = User::All();
        return view("users.list",Array("users"=>$users,"message"=>$message));
    }

    public function editUser($id){
        $user = User::findOrFail($id);
        return view("users.edit", Array("user"=>$user));
    }

    public function updateUser($id, Request $request){
        $user = User::findOrFail($id);

        if($request->input("name")!=null)$user->name = $request->input("name");
        if($request->input("email")!=email)$user->email = $request->input("email");

        $message = array("data"=>"User modified.","type"=>"success");
        try{
            $user->save();
        }catch(\Illuminate\Database\QueryException $ex){
            $message = array("data"=>"Exception trying modify the data ".$ex, "type"=>"danger");
        }
        return $this->getList($message);
    }

    public function changePassword($id){
        $user = User::findOrFail($id);
        return view("users.password", Array("user"=>$user));
    }

    public function updatePassword($id, Request $request){
        if($request->input("password") != $request->input("password_confirm"))
            return redirect()->back()->withInput()->with("message",Array("type"=>"danger","data"=>"Password fields does not match!"));

            $user = User::findOrFail($id);

            $user->password = bcrypt($request->input("password"));
            $message = array("data"=>"Password changed.","type"=>"success");

            try{
                $user->save();
            }catch(\Illuminate\Database\QueryException $ex){
                $message = array("data"=>"Exception trying modify the data ".$ex, "type"=>"danger");
            }
            return $this->getList($message);
    }

}