tethys/app/Http/Controllers/Settings/CategoryController.php

95 lines
2.6 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
namespace App\Http\Controllers\Settings;
use App\Http\Controllers\Controller;
use App\Http\Requests\ProjectRequest;
2018-09-10 13:09:10 +00:00
use App\Models\Project;
2018-08-06 12:30:51 +00:00
use Illuminate\Http\RedirectResponse;
2018-08-29 15:18:15 +00:00
use Illuminate\View\View;
2018-08-06 12:30:51 +00:00
class CategoryController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
2018-08-29 15:18:15 +00:00
public function index(): View
2018-08-06 12:30:51 +00:00
{
$projects = Project::get();
return view('settings.project.category', compact('projects'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
2018-08-29 15:18:15 +00:00
public function add(): View
2018-08-06 12:30:51 +00:00
{
return view('settings.project.add');
}
2018-08-29 15:18:15 +00:00
public function store(ProjectRequest $request): RedirectResponse
2018-08-06 12:30:51 +00:00
{
$input = $request->all();
Project::create($input);
session()->flash('flash_message', 'You have added 1 project!');
return redirect()->route('settings.project');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2018-08-29 15:18:15 +00:00
public function edit($id): View
2018-08-06 12:30:51 +00:00
{
$project = Project::findOrFail($id);
return view('settings.project.edit', compact('project'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
2018-08-29 15:18:15 +00:00
public function update($id, ProjectRequest $request): RedirectResponse
2018-08-06 12:30:51 +00:00
{
$project = Project::findOrFail($id);
$input = $request->all();
$project->update($input);
session()->flash('flash_message', 'You have updated 1 project!');
return redirect()->route('settings.project');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2018-08-29 15:18:15 +00:00
public function delete($id): RedirectResponse
2018-08-06 12:30:51 +00:00
{
$project = Project::with('documents')->findOrFail($id);
if ($project->documents->count() > 0) {
session()->flash(
'flash_message',
'You cannot delete this project!'
2018-08-29 15:18:15 +00:00
. ' There are '
. $project->documents->count()
. ' documents in this project!'
2018-08-06 12:30:51 +00:00
);
return redirect()->route('settings.project');
} else {
$project->delete();
session()->flash('flash_message', 'You have deleted 1 project!');
return redirect()->route('settings.project');
}
}
}