Explorar o código

Add profile model, migration and controller

Also added User model observer to create a profile when a new user is
created.
Daniel Supernault %!s(int64=7) %!d(string=hai) anos
pai
achega
9dd58c5abd

+ 10 - 0
app/Http/Controllers/ProfileController.php

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

+ 25 - 0
app/Observer/UserObserver.php

@@ -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 - 0
app/Profile.php

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

+ 3 - 1
app/Providers/AppServiceProvider.php

@@ -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);
     }
 
     /**

+ 5 - 0
app/User.php

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

+ 40 - 0
database/migrations/2018_04_16_002611_create_profiles_table.php

@@ -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');
+    }
+}