laravel-learn-bbs/app/Http/Controllers/TopicsController.php

64 lines
1.7 KiB
PHP
Raw Normal View History

2018-01-01 04:00:45 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\TopicRequest;
2018-01-01 08:32:20 +00:00
use App\Models\Category;
2018-01-01 04:00:45 +00:00
use App\Models\Topic;
2018-01-01 07:22:14 +00:00
use Illuminate\Http\Request;
2018-01-01 08:32:20 +00:00
use Illuminate\Support\Facades\Auth;
2018-01-01 04:00:45 +00:00
class TopicsController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
2018-01-01 08:32:20 +00:00
public function create(Topic $topic)
{
$categories = Category::all();
return view('topics.create_and_edit', compact('topic', 'categories'));
}
2018-01-01 07:22:14 +00:00
public function index(Request $request, Topic $topic)
2018-01-01 04:00:45 +00:00
{
2018-01-01 07:22:14 +00:00
$topics = $topic->with('user', 'category')->withOrder($request->order)->paginate();
2018-01-01 04:00:45 +00:00
return view('topics.index', compact('topics'));
}
public function show(Topic $topic)
{
return view('topics.show', compact('topic'));
}
2018-01-01 08:32:20 +00:00
public function store(TopicRequest $request, Topic $topic)
2018-01-01 04:00:45 +00:00
{
2018-01-01 08:32:20 +00:00
$topic->fill($request->all());
$topic->user_id = Auth::id();
$topic->save();
2018-01-01 04:00:45 +00:00
return redirect()->route('topics.show', $topic->id)->with('message', 'Created successfully.');
}
public function edit(Topic $topic)
{
$this->authorize('update', $topic);
return view('topics.create_and_edit', compact('topic'));
}
public function update(TopicRequest $request, Topic $topic)
{
$this->authorize('update', $topic);
$topic->update($request->all());
return redirect()->route('topics.show', $topic->id)->with('message', 'Updated successfully.');
}
public function destroy(Topic $topic)
{
$this->authorize('destroy', $topic);
$topic->delete();
return redirect()->route('topics.index')->with('message', 'Deleted successfully.');
}
}