Artem Petrov 5 лет назад
Родитель
Сommit
25d53def89

+ 49 - 2
app/Http/Controllers/PostsController.php

@@ -8,8 +8,55 @@
 
 namespace App\Http\Controllers;
 
+use App\Http\Requests\StorePostRequest;
+use App\Post;
+use Illuminate\Http\Request;
 
-class PostsController
+class PostsController extends Controller
 {
+    /**
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function index()
+    {
+        $posts = Post::all();
 
-}
+        return view('posts.index', [
+            'posts' => $posts,
+        ]);
+    }
+
+    /**
+     * @param Post $post
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function view(Post $post)
+    {
+        return view('posts.view', [
+            'post' => $post,
+        ]);
+    }
+
+    /**
+     * @param Post $post
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function edit(Post $post)
+    {
+        return view('posts.edit', [
+            'post' => $post,
+        ]);
+    }
+
+    /**
+     * @param Request $request
+     * @param Post $post
+     */
+    public function update(StorePostRequest $request, Post $post)
+    {
+        $post->fill($request->toArray());
+        $post->save();
+
+        return redirect(route('posts.index'));
+    }
+}

+ 3 - 3
app/Http/Requests/StorePostRequest.php

@@ -20,8 +20,8 @@ class StorePostRequest extends FormRequest
     public function rules()
     {
         return [
-            'title' => 'required',
-            'description' => 'required|max:10',
+            'title' => 'required|',
+            'description' => 'required|min:10|max:500',
         ];
     }
 
@@ -31,4 +31,4 @@ class StorePostRequest extends FormRequest
             'title.required' => 'Ты тупой'
         ];
     }
-}
+}

+ 7 - 3
app/Post.php

@@ -8,8 +8,12 @@
 
 namespace App;
 
+use Illuminate\Database\Eloquent\Model;
 
-class Post
+class Post extends Model
 {
-
-}
+    protected $fillable = [
+        'title',
+        'description',
+    ];
+}

+ 33 - 0
database/migrations/2018_09_07_182942_create_posts_table.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreatePostsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('posts', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('title');
+            $table->text('description');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('posts');
+    }
+}

+ 17 - 7
resources/views/posts/edit.blade.php

@@ -1,7 +1,17 @@
-<?php
-/**
- * Created by PhpStorm.
- * User: artem
- * Date: 11.09.18
- * Time: 20:58
- */
+<form method="post" action="{{route('posts.update', ['id' => $post->id])}}">
+
+    <input name="_method" type="hidden" value="PUT">
+
+    <div class="form-group">
+        <input name="title" value="{{$post->title}}"/>
+        <textarea name="description">{{$post->description}}</textarea>
+    </div>
+
+    <!-- Submit Field -->
+    <div class="form-group col-md-12">
+        <div class="pull-right">
+            <button>Save</button>
+        </div>
+    </div>
+
+</form>

+ 12 - 7
resources/views/posts/index.blade.php

@@ -1,7 +1,12 @@
-<?php
-/**
- * Created by PhpStorm.
- * User: artem
- * Date: 11.09.18
- * Time: 20:36
- */
+<div class="posts-table">
+    <ul>
+        @foreach ($posts as $post)
+            <li>
+                Title: {{ $post->title }}
+                <a href="{{route('posts.view', ['id' => $post->id])}}">View</a>
+                <a href="{{route('posts.edit', ['id' => $post->id])}}">Edit</a>
+
+            </li>
+        @endforeach
+    </ul>
+</div>

+ 4 - 7
resources/views/posts/view.blade.php

@@ -1,7 +1,4 @@
-<?php
-/**
- * Created by PhpStorm.
- * User: artem
- * Date: 11.09.18
- * Time: 20:42
- */
+<p>Title: {{$post->title}}</p>
+<p>Description: {{$post->description}}</p>
+<p>Created at: {{$post->created_at}}</p>
+<p>Updated at: {{$post->updated_at}}</p>

+ 4 - 5
routes/web.php

@@ -15,8 +15,7 @@ Route::get('/', function () {
     return view('welcome');
 });
 
-
-
-Route::middleware('api')->group(function () {
-    Route::apiResource('posts', 'PostsController');
-});
+Route::get('posts', 'PostsController@index')->name('posts.index');
+Route::get('posts/{post}', 'PostsController@view')->name('posts.view');
+Route::get('posts/{post}/edit', 'PostsController@edit')->name('posts.edit');
+Route::put('posts/{post}', 'PostsController@update')->name('posts.update');