12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Foundation\Bus\DispatchesJobs;
- use Illuminate\Routing\Controller as BaseController;
- use Illuminate\Foundation\Validation\ValidatesRequests;
- use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Input;
- use App\Post;
- use App\Tag;
- class SomeController extends BaseController
- {
- use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
- public function index(){
- $result = [];
- foreach(Post::all() as $post){
- $record = ["title" => $post->title,
- "text" => $post->text,
- "id" => $post->id];
- $record['commentCount'] = $post->comments()->count();
- $record['tags'] = $post->tags;
- $result[] = $record;
- }
- return view('post_index', ['posts' => $result]);
- }
- public function create(){
- return view('form', ['allTags' => Tag::all(), 'postTags' => []]);
- }
- public function edit($id){
- $post = Post::find($id);
- $postTags = [];
- foreach($post->tags as $tag){
- $postTags[] = $tag->id;
- }
- return view('form',['post' => $post,
- 'allTags' => Tag::all(),
- 'postTags' => $postTags]);
- }
- public function store(Request $req){
- $newRecord = new Post([ 'title' =>Input::get('title'), 'text' =>Input::get('text')]);
- $newRecord->save();
- return redirect(route('cntrl.index'));
- }
- public function update($id){
- $post = Post::find($id);
- $post->title = Input::get('title');
- $post->text = Input::get('text');
- $tagIds = Input::get('tags');
- $post->tags()->detach();
- $post->tags()->attach($tagIds);
- $post->save();
- return redirect(route('cntrl.index'));
- }
-
- public function destroy($id){
- Post::destroy($id);
- return redirect(route('cntrl.index'));
- }
- public function show($id){
- $post = Post::find($id);
- return view('post_show', ['post' => $post,
- "comments" => $post->comments()->orderBy('id')->get()]);
- }
- }
|