Forked from anonymous/2014_10_12_000000_create_users_table.php
Last active
December 9, 2017 19:04
-
-
Save bkilshaw/09594d108da1e67d906e9036abeef49a to your computer and use it in GitHub Desktop.
Help getting "invites" to work and clean up code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateUsersTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('users', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->integer('company_id')->unsigned(); | |
$table->string('first_name'); | |
$table->string('last_name'); | |
$table->string('email')->unique(); | |
$table->string('password'); | |
$table->string('status')->default(0); | |
$table->rememberToken(); | |
$table->timestamps(); | |
$table->foreign('company_id') | |
->references('id') | |
->on('companies'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('users'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateCompaniesTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('companies', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('name'); | |
$table->integer('owner_user_id')->unsigned()->nullable(); | |
$table->timestamps(); | |
$table->foreign('owner_user_id') | |
->references('users') | |
->on('id'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('companies'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Company extends Model | |
{ | |
protected $fillable = [ | |
'name', | |
]; | |
public function employees() { | |
return $this->hasMany(User::class); | |
} | |
public function owner() { | |
return $this->belongsTo(User::class, 'owner_user_id') | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<div class="panel panel-default"> | |
<div class="panel-heading">Dashboard</div> | |
<div class="panel-body"> | |
@if (session('status')) | |
<div class="alert alert-success"> | |
{{ session('status') }} | |
</div> | |
@endif | |
You are logged in! | |
</div> | |
<ul class="list-group"> | |
<li class="list-group-item"><strong>First name:</strong> {{ Auth::user()->first_name }}</li> | |
<li class="list-group-item"><strong>Last name:</strong> {{ Auth::user()->last_name }}</li> | |
<li class="list-group-item"><strong>Company:</strong> {{ Auth::user()->company->company_name }}</li> | |
</ul> | |
</div> | |
<div class="panel panel-default"> | |
<div class="panel-heading">Invite User</div> | |
<div class="panel-body"> | |
<form method="POST" action="{{ route('register') }}"> | |
{{ csrf_field() }} | |
<div class="form-group"> | |
<input id="company_name" type="hidden" name="company_name" value="not-used" class="form-control"> | |
<input id="first_name" type="hidden" name="first_name" value="not-provided" class="form-control"> | |
<input id="last_name" type="hidden" name="last_name" value="not-provided" class="form-control"> | |
<input id="password" type="hidden" name="password" value="random" class="form-control"> | |
<label for="email">Email address</label> | |
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email"> | |
<small id="emailHelp" class="form-text text-muted">An email will be sent to the user with an invite.</small> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<div class="panel panel-default"> | |
<div class="panel-heading">Register</div> | |
<div class="panel-body"> | |
<form class="form-horizontal" method="POST" action="{{ route('register') }}"> | |
{{ csrf_field() }} | |
<div class="form-group{{ $errors->has('company_name') ? ' has-error' : '' }}"> | |
<label for="company_name" class="col-md-4 control-label">Company</label> | |
<div class="col-md-6"> | |
<input id="company_name" type="text" class="form-control" name="company_name" value="{{ old('company_name') }}" required autofocus> | |
@if ($errors->has('company_name')) | |
<span class="help-block"> | |
<strong>{{ $errors->first('company_name') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> | |
<label for="first_name" class="col-md-4 control-label">Frist Name</label> | |
<div class="col-md-6"> | |
<input id="first_name" type="text" class="form-control" name="first_name" value="{{ old('first_name') }}" required autofocus> | |
@if ($errors->has('first_name')) | |
<span class="help-block"> | |
<strong>{{ $errors->first('first_name') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> | |
<label for="last_name" class="col-md-4 control-label">Last Name</label> | |
<div class="col-md-6"> | |
<input id="last_name" type="text" class="form-control" name="last_name" value="{{ old('last_name') }}" required autofocus> | |
@if ($errors->has('last_name')) | |
<span class="help-block"> | |
<strong>{{ $errors->first('last_name') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> | |
<label for="email" class="col-md-4 control-label">E-Mail Address</label> | |
<div class="col-md-6"> | |
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required> | |
@if ($errors->has('email')) | |
<span class="help-block"> | |
<strong>{{ $errors->first('email') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> | |
<label for="password" class="col-md-4 control-label">Password</label> | |
<div class="col-md-6"> | |
<input id="password" type="password" class="form-control" name="password" required> | |
@if ($errors->has('password')) | |
<span class="help-block"> | |
<strong>{{ $errors->first('password') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label> | |
<div class="col-md-6"> | |
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-md-6 col-md-offset-4"> | |
<button type="submit" class="btn btn-primary"> | |
Register | |
</button> | |
</div> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Auth; | |
use App\User; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Support\Facades\Validator; | |
use Illuminate\Foundation\Auth\RegistersUsers; | |
use \App\Company; | |
class RegisterController extends Controller | |
{ | |
/* | |
|-------------------------------------------------------------------------- | |
| Register Controller | |
|-------------------------------------------------------------------------- | |
| | |
| This controller handles the registration of new users as well as their | |
| validation and creation. By default this controller uses a trait to | |
| provide this functionality without requiring any additional code. | |
| | |
*/ | |
use RegistersUsers; | |
/** | |
* Where to redirect users after registration. | |
* | |
* @var string | |
*/ | |
protected $redirectTo = '/home'; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('guest'); | |
} | |
/** | |
* Get a validator for an incoming registration request. | |
* | |
* @param array $data | |
* @return \Illuminate\Contracts\Validation\Validator | |
*/ | |
protected function validator(array $data) | |
{ | |
return Validator::make($data, [ | |
'company_name' => 'required|string|max:255', | |
'first_name' => 'required|string|max:255', | |
'last_name' => 'required|string|max:255', | |
'email' => 'required|string|email|max:255|unique:users', | |
'password' => 'required|string|min:6|confirmed', | |
]); | |
} | |
/** | |
* Create a new user instance after a valid registration. | |
* | |
* @param array $data | |
* @return \App\User | |
*/ | |
protected function create(array $data) | |
{ | |
// Create a new company. Needs to happen first to get the company ID. | |
$company = Company::create(['company_name' => $data['company_name']]); | |
// Create the new user | |
$user = $company->users()->create([ | |
'first_name' => $data['first_name'], | |
'last_name' => $data['last_name'], | |
'email' => $data['email'], | |
'password' => bcrypt($data['password']), | |
]); | |
// Update the Company with the user ID to set owner | |
$company->owner()->associate($user); | |
$company->save(); | |
return $user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use App\Company; | |
class User extends Authenticatable | |
{ | |
use Notifiable; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'first_name','last_name','company_id', 'email', 'password','status', | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token', | |
]; | |
// Greats a relationship between a user and company | |
public function company() | |
{ | |
return $this->belongsTo(Company::Class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment