diff --git a/app/BlogPost.php b/app/BlogPost.php index 9c5331f..3781ea9 100644 --- a/app/BlogPost.php +++ b/app/BlogPost.php @@ -3,10 +3,10 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use SoftDeletes; class BlogPost extends Model { - use SoftDeletes; /** * The attributes that are mass assignable. diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 18a0d08..822dbc4 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -37,4 +37,12 @@ class LoginController extends Controller { $this->middleware('guest')->except('logout'); } + + /** + * Specify field that's used in the login form. + */ + public function username() + { + return 'username'; + } } diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index c6a6de6..b09b113 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -51,6 +51,7 @@ class RegisterController extends Controller { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], + 'username' => ['required', 'string', 'unique:users', 'min:3', 'max:30'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); @@ -66,6 +67,7 @@ class RegisterController extends Controller { return User::create([ 'name' => $data['name'], + 'username' => strtolower($data['username']), 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); diff --git a/app/Http/Controllers/BlogPostController.php b/app/Http/Controllers/BlogPostController.php index 47e4e0c..f9fd0da 100644 --- a/app/Http/Controllers/BlogPostController.php +++ b/app/Http/Controllers/BlogPostController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\BlogPost; +use App\User; use Illuminate\Http\Request; use App\Http\Request\BlogPostRequest; @@ -11,11 +12,17 @@ class BlogPostController extends Controller /** * Display a listing of the resource. * + * @param string $username * @return \Illuminate\Http\Response */ - public function index() + public function index($username) { - // + $user = User::where('username', $username) + ->firstOrFail(); + + return view('posts.index', [ + 'user' => $user, + ]); } /** diff --git a/app/User.php b/app/User.php index e78953d..4462d2b 100644 --- a/app/User.php +++ b/app/User.php @@ -16,7 +16,7 @@ class User extends Authenticatable * @var array */ protected $fillable = [ - 'name', 'email', 'password', + 'name', 'username', 'email', 'password', ]; /** diff --git a/database/factories/BlogPostFactory.php b/database/factories/BlogPostFactory.php index 1b970e9..6a2c7d4 100644 --- a/database/factories/BlogPostFactory.php +++ b/database/factories/BlogPostFactory.php @@ -7,6 +7,7 @@ use Faker\Generator as Faker; $factory->define(BlogPost::class, function (Faker $faker) { return [ - // + 'title' => $faker->sentence(5), + 'body' => $faker->paragraph(4), ]; }); diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index a91e1d3..3d36745 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -15,6 +15,10 @@ class CreateUsersTable extends Migration { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); + + $table->string('username', 30) + ->unique(); + $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); diff --git a/database/migrations/2020_03_03_115720_create_blog_posts_table.php b/database/migrations/2020_03_03_115720_create_blog_posts_table.php index 639d6ae..f26f815 100644 --- a/database/migrations/2020_03_03_115720_create_blog_posts_table.php +++ b/database/migrations/2020_03_03_115720_create_blog_posts_table.php @@ -14,7 +14,7 @@ class CreateBlogPostsTable extends Migration public function up() { Schema::create('blog_posts', function (Blueprint $table) { - $table->bigIncrementing('id'); + $table->bigIncrements('id'); $table->string('title', 150); $table->text('body'); diff --git a/database/seeds/BlogPostSeeder.php b/database/seeds/BlogPostSeeder.php index 6c6e0ab..a2af467 100644 --- a/database/seeds/BlogPostSeeder.php +++ b/database/seeds/BlogPostSeeder.php @@ -2,6 +2,9 @@ use Illuminate\Database\Seeder; +use App\BlogPost; +use App\User; + class BlogPostSeeder extends Seeder { /** @@ -11,6 +14,10 @@ class BlogPostSeeder extends Seeder */ public function run() { - // + User::all()->each(function ($user) { + $user->posts()->save( + factory(BlogPost::class)->make() + ); + }); } } diff --git a/public/js/app.js b/public/js/app.js index f530da0..82b0de0 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -49674,8 +49674,8 @@ __webpack_require__.r(__webpack_exports__); /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { -__webpack_require__(/*! A:\Projects\HovedprosjektWebApp\resources\js\app.js */"./resources/js/app.js"); -module.exports = __webpack_require__(/*! A:\Projects\HovedprosjektWebApp\resources\sass\app.scss */"./resources/sass/app.scss"); +__webpack_require__(/*! /home/alex/projects/HovedprosjektWebApp/resources/js/app.js */"./resources/js/app.js"); +module.exports = __webpack_require__(/*! /home/alex/projects/HovedprosjektWebApp/resources/sass/app.scss */"./resources/sass/app.scss"); /***/ }) diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index c12b97e..8ed51c5 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -12,12 +12,12 @@ @csrf
- +
- + - @error('email') + @error('username') {{ $message }} diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php index d236a48..72bd51d 100644 --- a/resources/views/auth/register.blade.php +++ b/resources/views/auth/register.blade.php @@ -25,6 +25,20 @@
+
+ + +
+ + + @error('username') + + {{ $message }} + + @enderror +
+
+
diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 05dfca9..8f1c1a9 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -14,7 +14,7 @@
@endif - You are logged in! + You are logged in, {{ Auth::user()->username }}! diff --git a/resources/views/posts/index.blade.php b/resources/views/posts/index.blade.php new file mode 100644 index 0000000..b00a96b --- /dev/null +++ b/resources/views/posts/index.blade.php @@ -0,0 +1,26 @@ +@extends('layouts.app') + +@section('content') +
+ @if ($user->posts->isEmpty()) + + @else + @foreach ($user->posts as $post) +
+

{{ $post->title }}

+

+ {{ nl2br($post->body) }} +

+

+ Created: {{ $post->created_at }} + @if ($post->created_at->diffInSeconds($post->updated_at) > 1) + | Last updated: {{ $post->updated_at }} + @endif +

+
+ @endforeach + @endif +
+@endsection diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 834f73f..e1dcf1a 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -2,8 +2,9 @@ @section('content')
-

Welcome to the {{ env('APP_NAME', 'Hovedprosjekt') }} homepage!

- - GASP +
+

Welcome to the {{ env('APP_NAME', 'Hovedprosjekt') }} homepage!

+ GASP +
@endsection diff --git a/routes/web.php b/routes/web.php index 12fc04c..75508e3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,3 +18,7 @@ Route::get('/', function () { Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); + +Route::group(['prefix' => 'posts'], function() { + Route::get('/u/{username}', ['uses' => 'BlogPostController@index']); +});