Ekapp/skolehjem/app/Http/Controllers/NewsController.php

157 lines
3.6 KiB
PHP
Raw Normal View History

2020-08-06 10:21:19 +00:00
<?php
namespace App\Http\Controllers;
2020-08-12 13:26:32 +00:00
use App\Helpers;
2020-08-06 10:21:19 +00:00
use App\News;
2020-08-12 13:26:32 +00:00
use App\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\RedirectResponse;
2020-08-06 10:21:19 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class NewsController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "check.auth:news.list" ])->only("index");
$this->middleware([ "check.auth:news.show" ])->only("show");
$this->middleware([ "check.auth:news.create" ])->only("create", "store");
$this->middleware([ "check.auth:news.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:news.delete" ])->only("delete");
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$news = News::query()->paginate($request->input("limit", 20));
return Response::detect("news.index", [ "news" => $news ]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return Response::detect("news.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
2020-08-12 13:26:32 +00:00
* @return RedirectResponse
2020-08-06 10:21:19 +00:00
*/
public function store(Request $request)
{
$data = $request->validate([
"subname" => "required",
2020-08-06 10:21:19 +00:00
"content" => "required"
]);
$news = new News($data);
$news->name = "";
$news->type_id = '1';
2020-08-06 10:21:19 +00:00
$news->save();
2020-08-12 13:26:32 +00:00
Helpers::sendNewsNotification($news, User::all());
2020-08-06 10:21:19 +00:00
return redirect()->route("news.index");
}
2020-08-12 13:26:32 +00:00
/**
* Store a newly created resource in storage.
*
* @return News
*/
public static function storeAndGet(News $news)
2020-08-12 13:26:32 +00:00
{
$news->save();
Helpers::sendNewsNotification($news, User::all());
return $news;
}
2020-08-06 10:21:19 +00:00
/**
* Display the specified resource.
*
* @param \App\News $news
* @return \Illuminate\Http\Response
*/
public function show(News $news)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\News $news
* @return \Illuminate\Http\Response
*/
public function edit(News $news)
{
return Response::detect("news.edit", [ "news" => $news ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\News $news
2020-08-12 13:26:32 +00:00
* @return RedirectResponse
2020-08-06 10:21:19 +00:00
*/
public function update(Request $request, News $news)
{
$data = $request->validate([
"subname" => "required",
2020-08-06 10:21:19 +00:00
"content" => "required"
]);
$news->update($data);
Helpers::sendNewsNotification($news, User::all());
2020-08-06 10:21:19 +00:00
return redirect()->route("news.index");
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\News $news
* @return RedirectResponse
*/
public static function updateAndGet(News $news)
{
$news->update();
Helpers::sendNewsNotification($news, User::all());
return $news;
}
2020-08-06 10:21:19 +00:00
/**
* Remove the specified resource from storage.
*
* @param \App\News $news
2020-08-12 13:26:32 +00:00
* @return RedirectResponse
2020-08-06 10:21:19 +00:00
* @throws \Exception
*/
public function destroy(News $news)
{
$news->delete();
return redirect()->route("news.index");
}
}