Skip to content

Commit 4b74b06

Browse files
committed
create news routes and pages
1 parent 36f1dfe commit 4b74b06

File tree

13 files changed

+213
-37
lines changed

13 files changed

+213
-37
lines changed

app/Http/Controllers/AdministrationController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\News;
56
use App\Post;
6-
use Illuminate\Http\Request;
77

88
class AdministrationController extends Controller
99
{
@@ -20,4 +20,9 @@ public function posts() {
2020
$posts = Post::with('tags')->latest()->get();
2121
return view('/admin.posts', compact('posts'));
2222
}
23+
24+
public function news() {
25+
$news = News::all();
26+
return view('/news.index', compact('news'));
27+
}
2328
}

app/Http/Controllers/NewsController.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,34 @@ public function index()
1515

1616
public function create()
1717
{
18-
// return view('news.create');
18+
return view('news.create');
1919
}
2020

2121
public function store(Request $request)
2222
{
2323
//
2424
}
2525

26-
public function show(News $news)
26+
public function show(News $new)
2727
{
28-
//
28+
return view('news.show', compact('new'));
2929
}
3030

31-
public function edit(News $news)
31+
public function edit(News $new)
3232
{
33-
//
33+
return view('news.edit', compact('new'));
3434
}
3535

3636
public function update(Request $request, News $news)
3737
{
38-
//
38+
$values = $request->validate([
39+
'name' => 'required|unique:news|max:100',
40+
'text' => 'required',
41+
]);
42+
43+
$news->update($values);
44+
45+
return back();
3946
}
4047

4148
public function destroy(News $news)

app/View/Components/NewForm.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\View\Components;
4+
5+
use Illuminate\View\Component;
6+
7+
class NewForm extends Component
8+
{
9+
public $new;
10+
11+
public function __construct($new)
12+
{
13+
$this->new = $new;
14+
}
15+
16+
public function render()
17+
{
18+
return view('components.new-form', ['new' => $this->new]);
19+
}
20+
}

database/migrations/2020_10_01_153124_create_news_table.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ public function up()
1515
{
1616
Schema::create('news', function (Blueprint $table) {
1717
$table->id();
18-
$table->string('name');
18+
$table->string('name', 100);
1919
$table->text('text');
2020
$table->timestamps();
2121
});
2222
}
2323

24-
/**
25-
* Reverse the migrations.
26-
*
27-
* @return void
28-
*/
2924
public function down()
3025
{
3126
Schema::dropIfExists('news');
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<div class="form__fields row d-flex flex-column">
2+
<div class="form__field col-6 mb-3">
3+
<label for="form-name">New Name</label>
4+
<input type="text"
5+
class="form-control @error('name') is-invalid @enderror"
6+
id="form-name"
7+
name="name"
8+
value="{{ old('name', $new->name) }}"
9+
required=""
10+
>
11+
12+
<div class="invalid-feedback">
13+
New Name is required.
14+
</div>
15+
16+
@error('name')
17+
<div class="alert alert-danger">
18+
{{ $message }}
19+
</div>
20+
@enderror
21+
</div>
22+
23+
<div class="form__field d-flex flex-column col-12 mb-3">
24+
<label for="form-text">Text</label>
25+
<textarea name="text"
26+
class="form-control @error('text') is-invalid @enderror"
27+
id="form-text"
28+
cols="30"
29+
rows="10"
30+
placeholder="New content here"
31+
required="">{{ old('text', $new->text) }}</textarea>
32+
33+
<div class="invalid-feedback">
34+
New Text is required.
35+
</div>
36+
37+
@error('text')
38+
<div class="alert alert-danger">
39+
{{ $message }}
40+
</div>
41+
@enderror
42+
</div>
43+
</div>

resources/views/components/post-form.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class="form-control @error('text') is-invalid @enderror"
7373
required="">{{ old('text', $post->text) }}</textarea>
7474

7575
<div class="invalid-feedback">
76-
Post Description is required.
76+
Post Text is required.
7777
</div>
7878

7979
@error('text')

resources/views/layouts/admin/admin-header.blade.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121
</li>
2222

2323
<li class="nav-item">
24-
<a class="nav-link" href="{{ route('feedback') }}">{{ __('Feedbacks') }}</a>
24+
<a class="nav-link" href="{{ route('admin.posts') }}">{{ __('Posts') }}</a>
2525
</li>
2626

2727
<li class="nav-item">
28-
<a class="nav-link" href="{{ route('admin.posts') }}">{{ __('Posts') }}</a>
28+
<a class="nav-link" href="{{ route('admin.news') }}">{{ __('News') }}</a>
29+
</li>
30+
31+
<li class="nav-item">
32+
<a class="nav-link" href="{{ route('news.create') }}">{{ __('Create New') }}</a>
33+
</li>
34+
35+
<li class="nav-item">
36+
<a class="nav-link" href="{{ route('feedback') }}">{{ __('Feedbacks') }}</a>
2937
</li>
3038
</ul>
3139

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@extends('layouts.app')
2+
3+
@section('header')
4+
@include('layouts.admin.admin-header')
5+
@endsection
6+
7+
@section('content')
8+
<main class="d-flex py-4" style="min-height: 88vh">
9+
<div class="container">
10+
<section class="new-create mb-2">
11+
<h2 class="new-create__header mb-4"><strong>Creating New</strong></h2>
12+
13+
<form class="new-create__form form d-flex flex-column needs-validation" method="post" action="{{ route('news.store') }}" novalidate>
14+
@csrf
15+
16+
<x-NewForm :new="new \App\News()"></x-NewForm>
17+
18+
<button class="btn btn-primary align-self-end" type="submit" style="width: 150px">Create</button>
19+
</form>
20+
</section>
21+
</div>
22+
</main>
23+
@endsection
24+
25+
26+
@section('footer')
27+
@include('layouts.admin.admin-footer')
28+
@endsection
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@extends('layouts.app')
2+
3+
@section('header')
4+
@include('layouts.admin.admin-header')
5+
@endsection
6+
7+
@section('content')
8+
<main class="d-flex py-4" style="min-height: 88vh">
9+
<div class="container">
10+
<section class="new-edit mb-2">
11+
<h2 class="new-edit__header mb-4"><strong>Edit new #{{ $new->id }}</strong></h2>
12+
13+
<form class="new-edit__form form d-flex flex-column needs-validation" method="post" action="{{ route('news.update', $new) }}" novalidate>
14+
@csrf
15+
@method('patch')
16+
17+
<x-NewForm :new="$new"></x-NewForm>
18+
19+
<button class="btn btn-primary align-self-end" type="submit" style="width: 150px">Save changes</button>
20+
</form>
21+
</section>
22+
</div>
23+
</main>
24+
@endsection
25+
26+
@section('footer')
27+
@include('layouts.admin.admin-footer')
28+
@endsection

resources/views/news/index.blade.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@
2020

2121
<div class="new__created-at mb-1 text-muted">{{ $new->created_at->toFormattedDateString() }}</div>
2222

23-
<p class="new__preview card-text flex-grow-1 text-justify"> {{ str_limit($new->text, $limit = 100, $end = '...') }} </p>
24-
</div>
23+
<p class="new__preview card-text flex-grow-1 text-justify"> {{ str_limit($new->text, $limit = 200, $end = '...') }} </p>
2524

26-
{{-- <div class="d-flex col-12 justify-content-end pr-2">--}}
27-
{{-- <a href="/news/{{ $new->id }}/edit" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Edit</a>--}}
25+
<div class="d-flex justify-content-end">
26+
<a href="{{ route('news.show', $new) }}" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Read</a>
2827

29-
{{-- <form method="new" action="{{ route('news.destroy', $new) }}">--}}
30-
{{-- @csrf--}}
31-
{{-- @method('DELETE')--}}
32-
{{-- <button type="submit" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Delete</button>--}}
33-
{{-- </form>--}}
34-
{{-- </div>--}}
28+
@if(auth()->user()->hasRole('admin'))
29+
<a href="{{ route('news.edit', $new) }}" class="btn btn-outline-secondary ml-1" style="width: 80px; font-size: 0.7rem">Edit</a>
30+
@endif
31+
</div>
32+
</div>
3533
</div>
3634
@empty
3735
<p class="no-news">No available news yet</p>

0 commit comments

Comments
 (0)