Skip to content

Commit

Permalink
Use custom database connection in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schöps committed Feb 17, 2021
1 parent 84059ec commit 67a9f2e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ Every user receives a random string which is stored in a cookie to identify them
## Installation
`composer require nanuc/laravel-track`

#### Publish config (optional)
`php artisan vendor:publish --provider="Nanuc\LaravelTrack\LaravelTrackServiceProvider" --tag=config`

You can configure a separate database connection.

#### Run migrations
Run `php artisan migrate` to create the necessary tables.


## Usage
### Middleware
Just add the middleware `track` to the routes you want to track.
Expand Down
5 changes: 5 additions & 0 deletions config/laravel-track.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'connection' => null,
];
8 changes: 8 additions & 0 deletions src/LaravelTrackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function boot()
{
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

$this->publishes([
__DIR__.'/../config/laravel-track.php' => config_path('laravel-track.php'),
], 'config');

Blade::directive('ab', function($expression) {
$temp = collect(explode(',', $expression))->map(function ($item) {
return str_replace(["'", '"'], '',trim($item));
Expand All @@ -28,5 +32,9 @@ public function register()
{
$this->app->singleton('track', fn() => new Tracker());
app('router')->aliasMiddleware('track', TrackRequest::class);

$this->mergeConfigFrom(
__DIR__.'/../config/laravel-track.php', 'laravel-track'
);
}
}
3 changes: 3 additions & 0 deletions src/Models/ABTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Nanuc\LaravelTrack\Models;

use Illuminate\Database\Eloquent\Model;
use Nanuc\LaravelTrack\Models\Traits\HasCustomConnection;

class ABTest extends Model
{
use HasCustomConnection;

protected $fillable = ['key'];

protected $table = 'track_ab_tests';
Expand Down
3 changes: 3 additions & 0 deletions src/Models/ABTestOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Nanuc\LaravelTrack\Models;

use Illuminate\Database\Eloquent\Model;
use Nanuc\LaravelTrack\Models\Traits\HasCustomConnection;

class ABTestOption extends Model
{
use HasCustomConnection;

protected $fillable = ['key', 'track_ab_test_id'];

protected $table = 'track_ab_test_options';
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Nanuc\LaravelTrack\Models;

use Illuminate\Database\Eloquent\Model;
use Nanuc\LaravelTrack\Models\Traits\HasCustomConnection;

class Campaign extends Model
{
use HasCustomConnection;

protected $table = 'track_campaigns';

public static function fromRequest()
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Goal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Nanuc\LaravelTrack\Models;

use Illuminate\Database\Eloquent\Model;
use Nanuc\LaravelTrack\Models\Traits\HasCustomConnection;

class Goal extends Model
{
use HasCustomConnection;

protected $table = 'track_goals';

protected $fillable = ['key'];
Expand Down
5 changes: 4 additions & 1 deletion src/Models/PageView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Illuminate\Database\Eloquent\Model;
use Nanuc\LaravelTrack\Facades\Tracker;
use Nanuc\LaravelTrack\Models\Traits\HasCustomConnection;

class PageView extends Model
{
use HasCustomConnection;

protected $table = 'track_page_views';

public function goals()
public function goals()
{
return $this->belongsToMany(Goal::class, 'track_page_view_goal', 'track_page_view_id', 'track_goal_id');
}
Expand Down
11 changes: 11 additions & 0 deletions src/Models/Traits/HasCustomConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Nanuc\LaravelTrack\Models\Traits;

trait HasCustomConnection
{
public function getConnectionName()
{
return config('laravel-track.connection');
}
}
3 changes: 3 additions & 0 deletions src/Models/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use Illuminate\Support\Str;
use Jenssegers\Agent\Agent;
use Nanuc\LaravelTrack\Facades\Tracker;
use Nanuc\LaravelTrack\Models\Traits\HasCustomConnection;

class Visitor extends Model
{
use HasCustomConnection;

protected $table = 'track_visitors';

public static function booted()
Expand Down

0 comments on commit 67a9f2e

Please sign in to comment.