Add profile model, migration and controller

Also added User model observer to create a profile when a new user is
created.
This commit is contained in:
Daniel Supernault 2018-04-15 18:52:22 -06:00
parent 2855c83c50
commit 9dd58c5abd
6 changed files with 93 additions and 1 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
//
}

View file

@ -0,0 +1,25 @@
<?php
namespace App\Observers;
use App\{Profile, User};
class UserObserver
{
/**
* Listen to the User created event.
*
* @param \App\User $user
* @return void
*/
public function saved(User $user)
{
if($user->has('profile')->count() == 0) {
$profile = new Profile;
$profile->user_id = $user->id;
$profile->username = $user->username;
$profile->save();
}
}
}

10
app/Profile.php Normal file
View file

@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
//
}

View file

@ -2,6 +2,8 @@
namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@ -13,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
User::observe(UserObserver::class);
}
/**

View file

@ -26,4 +26,9 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];
public function profile()
{
return $this->hasOne(Profile::class);
}
}

View file

@ -0,0 +1,40 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->nullable();
$table->string('username')->nullable()->unique()->index();
$table->string('name')->nullable();
$table->string('bio', 150)->nullable();
$table->string('location')->nullable();
$table->string('website')->nullable();
$table->string('remote_url')->nullable();
$table->text('keybase_proof')->nullable();
$table->boolean('is_private')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('profiles');
}
}