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

117 lines
2.8 KiB
PHP
Raw Normal View History

2020-08-06 10:21:19 +00:00
<?php
namespace App\Http\Controllers;
use App\News;
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
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
$data = $request->validate([
"name" => "required",
"content" => "required"
]);
$news = new News($data);
$news->save();
return redirect()->route("news.index");
}
/**
* 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
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, News $news)
{
$data = $request->validate([
"name" => "required",
"content" => "required"
]);
$news->update($data);
return redirect()->route("news.index");
}
/**
* Remove the specified resource from storage.
*
* @param \App\News $news
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function destroy(News $news)
{
$news->delete();
return redirect()->route("news.index");
}
}