Skip to content

Instantly share code, notes, and snippets.

@gdespirito
Created September 21, 2018 18:36
Show Gist options
  • Save gdespirito/e810841751d2ba7c3a2d78c83dae80fa to your computer and use it in GitHub Desktop.
Save gdespirito/e810841751d2ba7c3a2d78c83dae80fa to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePeopleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('people', function (Blueprint $table) {
$table->increments('id');
$table->boolean('is_company')->default(false);
$table->string('company_name')->nullable();
$table->string('company_rut')->nullable();
$table->text('company_notes')->nullable();
$table->string('first_name');
$table->string('last_name')->nullable();
$table->string('type', 30)->default('other')->nullable();
$table->date('birthdate')->nullable();
$table->string('password')->nullable();
$table->string('email')->nullable()->unique();
$table->string('rut')->nullable()->unique();
$table->string('landline_phone')->nullable();
$table->string('mobile_phone')->nullable();
$table->string('registration_ip')->nullable();
$table->string('author_id')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->timestamp('phone_verified_at')->nullable();
$table->text('public_notes')->nullable();
$table->text('private_notes')->nullable();
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('people');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateWorkersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('workers', function (Blueprint $table) {
$table->increments('id');
$table->integer('person_id')->unsigned()->unique();
$table->foreign('person_id')->references('id')->on('people');
$table->text('description')->nullable();
$table->string('role')->nullable();
$table->string('admin_role')->nullable();
$table->string('work_position')->nullable();
$table->text('private_notes')->nullable();
$table->text('public_notes')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('workers');
}
}
<?php
namespace App\Nova;
use Freshwork\RutField\RutField;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\HasOne;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\MorphMany;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;
class Person extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\Person';
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'complete_name';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
'first_name',
'last_name',
'rut',
'email',
'public_notes',
'private_notes'
];
public static function singularLabel()
{
return 'Persona';
}
public static function label()
{
return 'Personas';
}
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Nombre', 'first_name')
->sortable()
->rules('required', 'max:255'),
Text::make('Apellido', 'last_name')
->sortable(),
Boolean::make('Empresa', 'is_company'),
Text::make('Nombre empresa', 'company_name')
->hideFromIndex(),
RutField::make('RUT empresa', 'company_rut')
->hideFromIndex(),
Text::make('Email')
->sortable()
->rules('required', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
Password::make('Password')
->onlyOnForms()
->creationRules('required', 'string', 'min:6')
->updateRules('nullable', 'string', 'min:6'),
Text::make('Celular', 'mobile_phone')
->sortable(),
Text::make('IP', 'registration_ip')
->hideFromIndex(),
RutField::make('Rut'),
MorphMany::make('Address'),
Textarea::make('Notas públicas', 'public_notes'),
Textarea::make('Notas privadas', 'private_notes'),
HasMany::make('Arriendos', 'rentals', Rental::class),
HasOne::make('Perfil de Trabajador', 'worker', Worker::class),
Gravatar::make(),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
<?php
namespace App\Nova;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\HasOne;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\MorphMany;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;
class Worker extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\Worker';
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
];
public static function singularLabel()
{
return 'Staff';
}
public static function label()
{
return 'Staff';
}
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
belongsTo::make('Persona', 'person', Person::class),
MorphMany::make('Cuenta bancaria', 'bank_account', BankAccount::class),
Textarea::make('Notas', 'public_notes'),
Textarea::make('Notas privadas', 'private_notes'),
Text::make('Cargo', 'work_position'),
Select::make('Rol en administración', 'admin_role')->options([
'admin' => 'Administrador General',
'sales' => 'Ventas',
])->displayUsingLabels(),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class Person extends Authenticatable
{
use Notifiable;
const TYPE_UNIVERSITY = 'university';
const TYPE_PERSONAL = 'personal';
const TYPE_SCHOOL = 'schools';
const TYPE_PARTY_PRODUCER = 'party_producer';
const TYPE_MARKETING_AGENCY = 'marketing_agency';
const TYPE_EVENT_CENTER = 'event_center';
const TYPE_DJ = 'dj';
const TYPE_OTHER_COMPANY = 'company';
const TYPE_ORG = 'org';
const TYPE_OTHER = 'other';
const TYPE_BAR = 'bar';
const TYPE_RESTAURANT = 'restaurant';
protected $table = 'people';
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/*
|--------------------------------------------------------------------------
| Relations
|--------------------------------------------------------------------------
*/
public function worker()
{
return $this->hasOne(Worker::class);
}
public function address()
{
return $this->morphMany(Address::class, 'addressable');
}
public function rentals()
{
return $this->hasMany(Rental::class, 'client_id');
}
/*
|--------------------------------------------------------------------------
| Mutators
|--------------------------------------------------------------------------
*/
/**
* @return string
*/
public function getCompleteNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
/*
|--------------------------------------------------------------------------
| Scopes
|--------------------------------------------------------------------------
*/
/**
* @param $query
* @param $searchText
* @return mixed
*/
public function scopeSearch($query, $searchText)
{
$words = explode(' ', $searchText);
foreach ($words as $word) {
$query = $query->where(function ($query) use ($word) {
$wordAsRut = str_replace(['.', '-', ' '], '', $word);
return $query->where('rut', 'LIKE', "%$wordAsRut%")
->orWhere('first_name', 'LIKE', "%$word%")
->orWhere('last_name', 'LIKE', "%$word%")
->orWhere('private_notes', 'LIKE', "%$word%")
->orWhere('public_notes', 'LIKE', "%$word%")
->orWhere('company_name', 'LIKE', "%$word%")
->orWhere('mobile_phone', 'LIKE', "%$word%")
->orWhere('email', 'LIKE', "%$word%");
});
}
return $query;
}
/*
|--------------------------------------------------------------------------
| Custom Methods
|--------------------------------------------------------------------------
*/
public function isSuperAdmin()
{
return $this->worker && $this->worker->admin_role === 'admin';
}
public function hasPanelAccess()
{
return $this->worker && $this->worker->admin_role !== null;
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Worker extends Model
{
const ROLE_EXTERNAL = 'external_staff';
const ROLE_EMPLOYEE = 'employee';
const ADMIN_ROLE_ADMIN = 'admin';
const ADMIN_ROLE_SALES = 'sales';
/*
|--------------------------------------------------------------------------
| Relations
|--------------------------------------------------------------------------
*/
public function person()
{
return $this->belongsTo(Person::class);
}
public function bank_account()
{
return $this->morphMany(BankAccount::class, 'accountable');
}
/*
|--------------------------------------------------------------------------
| Scopes
|--------------------------------------------------------------------------
*/
public function scopeSales($query)
{
return $query->where('admin_role', 'sales');
}
public function scopeAdmin($query)
{
return $query->where('admin_role', 'admin');
}
/*
|--------------------------------------------------------------------------
| Mutators
|--------------------------------------------------------------------------
*/
public function getNameAttribute()
{
return $this->person->name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment