dpicx06888 2014-02-05 17:16
浏览 28
已采纳

在laravel中向sentry2添加另一个表

Hi I would like to add another table to relate to the sentry users table. I can not seem to find any information on doing this.

I would like to have a table that stores user information and recent activity, lets call it user_activity and have the table be related by user_id.

How can I add this table so that it can be accessed using the sentry service provider?

  • 写回答

1条回答 默认 最新

  • doubo6658 2014-02-05 17:28
    关注

    You don't really need to think about Sentry in this case, Setry's tables are common tables like any other one, so you can just:

    php artisan migrate:make create_user_activity_table
    

    And create your table relating to users:

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    
    class CreateUserActivityTable extends Migration {
    
        public function up()
        {
            Schema::create('user_activity', function($table) {
                $table->increments('id');
                $table->unsignedInteger('user_id');
                $table->string('<column_name>');
                $table->timestamps();
            });
    
            Schema::table('user_activity', function($table) {
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            });
        }
    
        public function down()
        {
            Schema::drop('user_activity');
        }
    
    }
    

    EDIT

    To use and create your relations, it you can publish Sentry's config:

    php artisan config:publish cartalyst/sentry
    

    Edit app/config/packages/cartalyst/sentry/config.php and change

    'model' => 'Cartalyst\Sentry\Users\Eloquent\User',
    

    To your own model (which may be at app/models/User.php):

    'model' => 'User',
    

    But your model will have to now extend Sentry's original:

    class User extends Cartalyst\Sentry\Users\Eloquent\User {
    
        public function activities()
        {
            return $this->hasMany('UserActivity');
        }
    
    }
    

    And create a model for your new table:

    class UserActivity extends \Eloquent {
    
        protected $table = 'user_activity';
    
        public function user()
        {
            return $this->belongsTo('User');
        }
    
    }
    

    And now you can:

    $user = User::findById(1);
    
    foreach($user->activities as $activity)
    {
       echo $activity->description;
       echo $activity->user->name;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。