Laravel登录,检查用户名和密码是否与我的数据库匹配

时间:2017-11-29 05:43:22

标签: javascript php sqlite laravel-5

在我的登录表单中我需要的全部内容 只是用户名和密码,试图使用ajax从sqlite获取数据。

enter image description here

.AjAX of login.blade.php

    $(文件)。就绪(函数(){

  $("#login").click(function(){
    $.ajax({
      type: "GET",
      url: "UserController.php",
      data: { username: $("#username").val(), password: $("#password").val()},
      success: function(data){

        response = JSON.parse(data);
        if(response[0]){

        }
        else{
          alert(response[1]);
        }
      }
    });
  });

});

我的User.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class User extends Model
{
    protected $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'phone','email', 'password','password_confirmation','librarian'
    ];


}

在UserController中读取方法

//to read data from database to login
    public function read(Request $request){


        $results = DB::select('select * from users where id = :id', ['id' => 1]);

        foreach ($users as $user) {
    echo $user->username;
}

my web.php

Route::get('/login', function () {
    return view('login');
});
Route::get('/signup',function(){
    return view('signup');
});
Route::get('/bookscreate',function(){
return view('bookscreate');
});

Route::get('/','BookController@index');
Route::post('signup', 'UserController@store');
//Route::post('signup', 'UserController@read');

Route::resource('/books','BookController');

所以我的目标是去我的数据库表检查用户名和密码,如果它们匹配一个DB id然后允许用户登录。

1 个答案:

答案 0 :(得分:0)

https://laravel.com/docs/5.5/authentication

阅读laravel认证文档,因为laravel已经内置了您的要求,并且非常容易实现。谢谢,因为我无法发表评论,我不得不将其作为答案发布。