如何在laravel中录制用户的设备

时间:2017-02-27 05:42:17

标签: laravel laravel-5

如果用户在登录时访问其中一个设备,如何添加数据设备(平板电脑,手机,桌面),则会记录数据。

我使用包jenssegers / agent

设备,其列为:device_name,date

我试试,但不能记录在表

这是我在LoginController.php中的代码

   public function check(){
      $agent = new Agent();
      $dt = Carbon::now();

      if ( $agent->isDesktop() == true )
      {
        DB::table('device')->insert([
           ['device_name' => 'desktop', 'tgl' => $dt->toDateString()],
       ]);
      }

      elseif ($agent->isTablet() == true) {
        DB::table('device')->insert([
           ['device_name' =>'tablet', 'tgl' => $dt->toDateString()],
       ]);
      }

      elseif ($agent->isMobile() == true) {
        DB::table('device')->insert([
           ['device_name' => 'mobile', 'tgl' => $dt->toDateString()],
       ]);
     }

    }

请帮助我,非常感谢...

2 个答案:

答案 0 :(得分:1)

尝试以下代码,您的代码似乎没有进入任何if-else语句?

public function check(){
  $agent = new Agent();
    $dt = Carbon::now();

    $device_name = '';
    if ($agent->isDesktop()) {
        $device_name = 'desktop';
    } elseif ($agent->isTablet()) {
        $device_name = 'tablet';
    } elseif ($agent->isMobile()) {
        $device_name = 'mobile';
    }

    DB::table('device')->insert([
        ['device_name' => $device_name, 'tgl' => $dt->toDateString()],
    ]);

}

答案 1 :(得分:0)

这是我的完整代码LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Model\Management\Radcheck;
use Illuminate\Support\Facades\Lang;
use Debugbar;
use Jenssegers\Agent\Agent;
use Date;
use Carbon\Carbon;

class LoginController extends Controller
{

    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;





    protected function sendFailedLoginResponse(Request $request)
   {

       if ( ! Radcheck::where('username', $request->username)->first() ) {
           return redirect()->back()
               ->withInput($request->only($this->username(), 'remember'))
               ->withErrors([
                   $this->username() => Lang::get('auth.username'),

               ]);
       }

       if ( ! Radcheck::where('username', $request->username)->where('password', bcrypt($request->password))->first() ) {
           return redirect()->back()
               ->withInput($request->only($this->username(), 'remember'))
               ->withErrors([
                   'password' => Lang::get('auth.password'),
                   /*Debugbar::error('Put a message for debugging any error'),*/
               ]);
       }

       if ( ! Radcheck::where('username', $request->username)->where('password', bcrypt($request->password))->where('flag','=','not active')->first() ) {
           return redirect()->back()
               ->withInput($request->only($this->username(), 'remember'))
               ->withErrors([
                   'active' => Lang::get('auth.active'),
               ]);
       }


   }





   protected function credentials(Request $request)
   {
       /*return $request->only($this->username(), 'password');*/
       $crendentials = $request->only($this->username(), 'password');
       $crendentials['flag']='active';
       return $crendentials;
   }
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    public function username()
    {
        return 'username';
    }

    public function check(){
  $agent = new Agent();
    $dt = Carbon::now();

    $device_name = '';
    if ($agent->isDesktop()) {
        $device_name = 'desktop';
    } elseif ($agent->isTablet()) {
        $device_name = 'tablet';
    } elseif ($agent->isMobile()) {
        $device_name = 'mobile';
    }

    DB::table('device')->insert([
        ['device_name' => $device_name, 'tgl' => $dt->toDateString()],
    ]);

}

}