dotinstallのlaravel入門でブログを作っています。
現在一覧の表示と新規投稿機能のみ実装してあります。
投稿はtitleとbodyのinput欄のみです。
問題ですがバリデーションを作成してからtitle,bodyとも入力すると、通常通り投稿はできますが、
どちらも空で送信すると以下のエラーが発生します。
PHP
1SQLSTATE[23000]: Integrity constraint violation: 19 posts.title may not be NULL (SQL: insert into "posts" ("title", "body", "updated_at", "created_at") values (, , 2018-05-19 14:00:03, 2018-05-19 14:00:03))
dumpやddで$request->all()で確認しようと思ったのですが、dumpやddは出力されずにエラーメッセージのみしか表示されません。
どんなことが考えられるでしょうか?
web.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', 'PostsController@index'); // Route::get('/posts/{id}', 'PostsController@show'); Route::get('/posts/{post}', 'PostsController@show')->where('post', '[0-9]+'); Route::get('/posts/create', 'PostsController@create'); Route::post('/posts', 'PostsController@store');
PostsController.php
?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Post; class PostsController extends Controller { public function index() { // $posts = \App\Post::all(); // $posts = Post::all(); // $posts = Post::orderBy('created_at', 'desc')->get(); $posts = Post::latest()->get(); // $posts = []; // dd($posts->toArray()); // dump die // return view('posts.index', ['posts' => $posts]); return view('posts.index')->with('posts', $posts); } // public function show($id) { public function show(Post $post) { // $post = Post::find($id); // $post = Post::findOrFail($id); return view('posts.show')->with('post', $post); } public function create() { return view('posts.create'); } public function store(Request $request) { $this->validate($request, [ 'title' => 'required|min:3', 'body' => 'required' ]); $post = new Post(); $post->title = $request->title; $post->body = $request->body; $post->save(); return redirect('/'); } }
create.blade.php
@extends('layouts.default') @section('title', 'New Post') @section('content') <h1> <a href="{{ url('/') }}" class="header-menu">Back</a> New Post </h1> <form method="post" action="{{ url('/posts') }}"> {{ csrf_field() }} <p> <input type="text" name="title" placeholder="enter title" value="{{ old('title') }}"> @if ($errors->has('title')) <span class="error">{{ $errors->first('title') }}</span> @endif </p> <p> <textarea name="body" placeholder="enter body">{{ old('body') }}</textarea> @if ($errors->has('body')) <span class="error">{{ $errors->first('body') }}</span> @endif </p> <p> <input type="submit" value="Add"> </p> </form> @endsection
コードはdotinstallのコードを全てコピーしてみたのですがうまくいきません。
サイトにも質問しているのですが対応が遅くこちらで質問させていただきました。