laravel 5.2 - 为用户创建密码

时间:2016-05-04 18:50:21

标签: laravel hash passwords

我想实现后端用户管理,但如何为用户设置密码?

我的控制器:

public function store(Request $request)
{
    $this->validate($request, ['email' => 'required', 'name' => 'required', 'password' => 'required', 'surname' => 'required', ]);


    $user = new User($request->all());
    $user->password=bcrypt($request);
    $user->save();


    return redirect('dash/users');
}

查看

        <div class="form-group {{ $errors->has('password') ? 'has-error' : ''}}">
            {!! Form::label('password', trans('users.password'), ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::text('password', null, ['class' => 'form-control', 'required' => 'required']) !!}
                {!! $errors->first('password', '<p class="help-block">:message</p>') !!}
            </div>
        </div>

现在已修复,函数工作和我的新用户都存储在数据库中,但是当我尝试使用它们登录时,loginform会说“我找不到任何记录” 为什么呢?

4 个答案:

答案 0 :(得分:0)

您需要将密码文本传递给bcrypt而不是整个$ request

$user->password=bcrypt('yourpasswordtext');

答案 1 :(得分:0)

<?php

namespace App\Http\Controllers\Dash;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Auth;
use App\User;

use App\Report;
use App\Category;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Session;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return void
     */
    public function index()
    {
        $users = User::paginate(15);

        return view('dash.users.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return void
     */
    public function create()
    {
        return view('dash.users.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return void
     */

    public function store(Request $request)
    {
        $this->validate($request, ['email' => 'required', 'name' => 'required', 'password' => 'required', 'surname' => 'required', ]);


        $user = new User($request->all());
        $user->password = bcrypt($request);
        $user->save();

        return redirect('dash/users');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function show($id)
    {
        $user = User::findOrFail($id);

        return view('dash.users.show', compact('user'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function edit($id)
    {
        $user = User::findOrFail($id);

        return view('dash.users.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function update($id, Request $request)
    {
        $this->validate($request, ['email' => 'required', 'name' => 'required', 'password' => 'required', 'surname' => 'required', ]);

        $user = User::findOrFail($id);
        $user->update($request->all());

        Session::flash('flash_message', 'User updated!');

        return redirect('dash/users');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function destroy($id)
    {
        User::destroy($id);

        Session::flash('flash_message', 'User deleted!');

        return redirect('dash/users');
    }
}

更新,这是控制器

答案 2 :(得分:0)

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

/* ruote for Admin */
Route::group(['middleware' => ['web']], function () {
    Route::resource('dash/reports', 'Dash\\ReportsController');
});
Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/categories', 'Dash\\CategoriesController');
});

Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/roles', 'Dash\\RolesController');
});
Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/permissions', 'Dash\\PermissionsController');
});
Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/users', 'Dash\\UsersController');
});

/* another routes */
Route::auth();
Route::get('/provola', 'HomeController@autorizzazione');
Route::get('/home', 'HomeController@index');
Route::get('/', function () {return view('welcome');});


/* injection for user roles 

Route::get('/start', 'HomeController@inject');
*/

我的路线

答案 3 :(得分:0)

对于Laravel 5.6 +

密码应使用Hash::make($password)

进行哈希处理