如何使用电子邮件和密码登录时在数据库中创建登录历史记录?

时间:2016-11-05 04:35:41

标签: php laravel laravel-5.2

我想创建登录历史记录,当用户登录我的site.login历史记录时,应该保存在表login_history中。

迁移代码如下:

<?php

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

class LoginHistory extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('login_history', function (Blueprint $table) {

           $table->increments('id');
           $table->integer('user_id');
           $table->string('ip')->default(false);
           $table->timestamps();
           $table->softDeletes();
        });
    }

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

用户登录如下:

@extends('site')
{{-- Web site Title --}}
@section('title') {{{ trans('site/user.login') }}} :: @parent @stop
{{-- Content --}}
@section('content')
<article>
    <div class="container">
        <div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="panel-title">Login</div>
                        <div style="float:right; font-size: 80%; position: relative; top:-20px">
                            <a target="blank" href="{{ url('password/email')}}">
                                Forgot password?
                            </a>
                        </div>
                    </div>     
                    <div style="padding-top:30px" class="panel-body" >
                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12">
                        </div>
                        @include('errors.list')
                            <form class="form-horizontal" role="form" method="POST" action="{{ URL::to('/user/login') }}" autocomplete="off">
                                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                                <div style="margin-bottom: 25px" class="input-group">
                                    <span class="input-group-addon">
                                        <i class="glyphicon glyphicon-user"></i>
                                    </span> 
                                    <input type="email" class="form-control" required placeholder="enter your login email" name="email" value="{{ old('email') }}">                                      
                                </div>
                                <div style="margin-bottom: 25px" class="input-group">
                                    <span class="input-group-addon">
                                        <i class="glyphicon glyphicon-lock"></i>
                                    </span>
                                    <input type="password" required placeholder="enter your login password" class="form-control" name="password">
                                </div>
                                <div class="input-group">
                                    <div class="checkbox">
                                        <label>
                                            <input id="login-remember" type="checkbox" name="remember" value="1">
                                            Remember me
                                        </label>
                                    </div>
                                </div>
                                <div style="margin-top:10px" class="form-group">
                                    <div class="col-sm-12 controls">
                                        <button type="submit" class="btn btn-default" >
                                            Login
                                        </button>
                                        <!-- <a id="btn-fblogin" href="#" class="btn btn-primary">Login with Facebook</a> -->
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-12 control">
                                        <div style="border-top: 1px solid #ccc; padding-top:15px; font-size:85%" >
                                            Don't have an account!
                                            <a href="{{ url('register') }}">Create an account</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>        
                </div>
            </div>
        </div>
    </article>
@endsection

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是像这里扩展Auth ServiceProvider:https://laravel.com/docs/5.0/extending#authentication

在check函数中,插入一个新的login_history行。