Skip to content

Commit

Permalink
Model
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-betalabs committed Sep 14, 2018
1 parent 578fb6a commit 52f6165
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/Models/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use App\Scopes\Tenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Foo extends Model
{
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();

static::addGlobalScope(Tenant::class);
}

}
23 changes: 23 additions & 0 deletions app/Scopes/Tenant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class Tenant implements Scope
{

/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->where('tenant_id', \Auth::user()->id);
}
}
34 changes: 34 additions & 0 deletions database/migrations/2018_09_14_215344_create_foos_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

class CreateFoosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('foos', function (Blueprint $table) {
$table->increments('id');
$table->string('baz');
$table->unsignedInteger('tenant_id');
$table->foreign('tenant_id')->on('tenants')->references('id');
$table->timestamps();
});
}

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

0 comments on commit 52f6165

Please sign in to comment.