Add view for posts

Add login with username instead of email

Add blog post seeder with Factory
This commit is contained in:
Alex Thomassen 2020-03-05 13:36:17 +01:00
parent b52915b8f4
commit 9a4030f19a
Signed by: Alex
GPG Key ID: 10BD786B5F6FF5DE
16 changed files with 90 additions and 16 deletions

View File

@ -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.

View File

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

View File

@ -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']),
]);

View File

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

View File

@ -16,7 +16,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'username', 'email', 'password',
];
/**

View File

@ -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),
];
});

View File

@ -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();

View File

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

View File

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

4
public/js/app.js vendored
View File

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

View File

@ -12,12 +12,12 @@
@csrf
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<label for="username" class="col-md-4 col-form-label text-md-right">Username</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
<input id="username" type="username" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>
@error('email')
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>

View File

@ -25,6 +25,20 @@
</div>
</div>
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">Username</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required>
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

View File

@ -14,7 +14,7 @@
</div>
@endif
You are logged in!
You are logged in, <code>{{ Auth::user()->username }}</code>!
</div>
</div>
</div>

View File

@ -0,0 +1,26 @@
@extends('layouts.app')
@section('content')
<div class="container">
@if ($user->posts->isEmpty())
<div class="alert alert-info" role="alert">
<strong>{{ $user->name }}</strong> has no posts. Check back later :)
</div>
@else
@foreach ($user->posts as $post)
<div class="jumbotron">
<h3>{{ $post->title }}</h3>
<p>
{{ nl2br($post->body) }}
</p>
<p class="text-muted">
Created: {{ $post->created_at }}
@if ($post->created_at->diffInSeconds($post->updated_at) > 1)
| Last updated: {{ $post->updated_at }}
@endif
</p>
</div>
@endforeach
@endif
</div>
@endsection

View File

@ -2,8 +2,9 @@
@section('content')
<div class="container">
<h1>Welcome to the {{ env('APP_NAME', 'Hovedprosjekt') }} homepage!</h1>
<img src="/media/finsrud.png" alt="GASP">
<div class="text-center">
<h1>Welcome to the {{ env('APP_NAME', 'Hovedprosjekt') }} homepage!</h1>
<img src="/media/finsrud.png" alt="GASP">
</div>
</div>
@endsection

View File

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