2017-02-27 3 views
0

ログオン時にユーザーがデバイスの1つにアクセスした場合、データデバイス(タブレット、モバイル、デスクトップ)を追加する方法、次にデータが記録されました。ユーザーのデバイスをlaravelに記録する方法

iは、パッケージjenssegers /エージェントを使用し、カラム持つ

テーブルデバイス:デバイス名、日付を

私がしようとしますが、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()], 
     ]); 
    } 

    } 

大変ありがとうございます...

+0

エラーは何ですか? – EddyTheDove

+0

@eddy、エラーはありませんが、テーブルには記録できません –

+0

あなたのテーブルは 'device'または' devices'ですか?タイムスタンプ 'created_at'と' updated_at'を持っていますか? – EddyTheDove

答えて

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()], 
    ]); 

} 
+0

値$ agent-> isDesktop()は、ユーザーがデスクトップを使用してアクセスする場合はtrueです。等... –

+0

上記のコードを使用して挿入できますか? – SteD

+0

テーブルに挿入できません...レコードがありません –

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()], 
    ]); 

} 

} 
+1

'if()else'の直前に、行を挿入して行を挿入できるかどうかを確認してください。 'DB :: table( 'device') - > insert(['device_name' => 'デスクトップ'、 'tgl' => Carbon :: now() - > toDateString()]);'少なくとも私たちは問題がDB挿入から来ていないことを知っています。 – EddyTheDove

+0

@EddyTheDove、SOLVED ...テーブルで列挙型の値を入力するのは間違っています....ありがとうございました。 –

関連する問題