aboutsummaryrefslogtreecommitdiff
path: root/app/Http/Controllers/NewsController.php
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2020-11-02 17:45:41 +0100
committerMateja <mail@matejamaric.com>2020-11-02 17:45:41 +0100
commitcdd9fcf7bf49e4468c2290e30a98c6730a334900 (patch)
tree552b1372802e38b13ac6567845a35617e38a4f9c /app/Http/Controllers/NewsController.php
parentb5afd40a49a2c2b061865a501103ea2a060f4997 (diff)
downloadyota-laravel-cdd9fcf7bf49e4468c2290e30a98c6730a334900.tar.gz
yota-laravel-cdd9fcf7bf49e4468c2290e30a98c6730a334900.zip
News system finished...
Diffstat (limited to 'app/Http/Controllers/NewsController.php')
-rw-r--r--app/Http/Controllers/NewsController.php63
1 files changed, 59 insertions, 4 deletions
diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php
index d44ebd9..306066c 100644
--- a/app/Http/Controllers/NewsController.php
+++ b/app/Http/Controllers/NewsController.php
@@ -5,6 +5,9 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use function dd;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Support\Facades\Redirect;
+
use App\Models\Post;
class NewsController extends Controller
@@ -32,6 +35,8 @@ class NewsController extends Controller
public function create()
{
//
+ $data = Post::orderBy('created_at', 'desc')->get();
+ return view('pages.news-admin', compact('data'));
}
/**
@@ -42,7 +47,29 @@ class NewsController extends Controller
*/
public function store(Request $request)
{
- //
+ $rules = [
+ 'title' => 'required',
+ 'text' => 'required'
+ ];
+ $messages = [
+ 'sign.required' => 'You need to provide a title!',
+ 'text.required' => 'Your post needs to have some information!'
+ ];
+ $validator = Validator::make($request->all(), $rules, $messages);
+
+ if ($validator->fails()) {
+ return Redirect::back()
+ ->withErrors($validator)
+ ->withInput();
+ }
+
+ $post = new Post();
+ $post->title = $request->title;
+ $post->author = $request->author;
+ $post->text = $request->text;
+ $post->saveOrFail();
+
+ return Redirect::back()->with('status', "New post added.");
}
/**
@@ -64,7 +91,8 @@ class NewsController extends Controller
*/
public function edit($id)
{
- //
+ $data = Post::findOrFail($id);
+ return view('pages.editpost', compact('data'));
}
/**
@@ -76,7 +104,31 @@ class NewsController extends Controller
*/
public function update(Request $request, $id)
{
- //
+ if ($request->input('submit') == 'Edit post') {
+ $rules = [
+ 'title' => 'required',
+ 'text' => 'required'
+ ];
+ $messages = [
+ 'sign.required' => 'You need to provide a title!',
+ 'text.required' => 'Your post needs to have some information!'
+ ];
+ $validator = Validator::make($request->all(), $rules, $messages);
+
+ if ($validator->fails()) {
+ return Redirect::back()
+ ->withErrors($validator)
+ ->withInput();
+ }
+
+ $post = Post::findOrFail($id);
+ $post->title = $request->title;
+ $post->author = $request->author;
+ $post->text = $request->text;
+ $post->saveOrFail();
+
+ return Redirect::route('newsAdd')->with('statusE', "Post edited.");
+ } else return Redirect::route('newsAdd');
}
/**
@@ -87,6 +139,9 @@ class NewsController extends Controller
*/
public function destroy($id)
{
- //
+ $post = Post::findOrFail($id);
+ $title = $post->title;
+ $post->delete();
+ return Redirect::back()->with('statusE', "Post \"$title\" deleted.");
}
}