実現したいこと
投稿機能を実現させようとしていたところ、routeに関するエラーが発生したので修正したところ、今度はカラムにデータを入れられないエラーが発生しました。
routeの書き方がいけないのでしょうか?
それともcontroller関連の投稿を生成する箇所に不備があったのでしょうか?
発生している問題・エラーメッセージ
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null INSERT INTO `posts` ( `user_id`, `name`, `image`, `age`, `gender`, `explanation`, `updated_at`, `created_at` ) VALUES ( 1, ?, ?, ?, ?, ?, 2023 -08 -23 02: 32: 18, 2023 -08 -23 02: 32: 18 )
該当のソースコード
Post.php
1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Factories\HasFactory; 6use Illuminate\Database\Eloquent\Model; 7 8class Post extends Model 9{ 10 use HasFactory; 11 protected $fillable = ['id', 'user_id', 'name', 'image', 'age', 'gender', 'explanation']; 12 13 //1対多のリレーション追加 14 public function user() { 15 return $this->belongsTo(User::class); 16 } 17}
PostController
1<?php 2 3namespace App\Http\Controllers; 4use App\Models\Post; 5use Illuminate\Http\Request; 6use Illuminate\Support\Facades\Auth; 7 8class PostController extends Controller 9{ 10 public function index() 11 { 12 // データベース内のすべてのpostを取得し、post変数に代入 13 $posts = Post::all(); 14 // 'posts'フォルダ内の'index'viewファイルを返す。 15 // その際にview内で使用する変数を代入します。 16 return view('posts/index', ['posts' => $posts]); 17 } 18 19 public function show($id) 20 { 21 $post = Post::find($id); // idでPostを探し出す 22 return view('posts.show', ['post' => $post]); 23 } 24 25 public function create () 26 { 27 return view('posts/create'); 28 } 29 30 public function new() 31 { 32 return view('post/new'); 33 34 } 35 36 public function destroy($id) 37 { 38 $post = Post::find($id); 39 $post->delete(); 40 return redirect('/posts'); 41 } 42 43 public function store(Request $request) 44 { 45 //ユーザーIDを取得 46 $user_id = Auth::id(); 47 // 新しい Item を作成 48 $post = new Post; 49 // フォームから送られてきたデータをそれぞれ代入 50 $post->user_id = $user_id; 51 $post->name = $request->name; 52 $post->image = $request->image; 53 $post->age = $request->age; 54 $post->gender = $request->gender; 55 $post->explanation = $request->explanation; 56 // データベースに保存 57 $post->save(); 58 // indexページへ遷移 59 return redirect('/posts'); 60 } 61}
posts/create.blade.php
1@extends('layouts.app') 2 3@section('content') 4<div class="container"> 5 <div class="row justify-content-center"> 6 <div class="col-md-8"> 7 <div class="card"> 8 <div class="card-header">{{ __('Post_Create') }}</div> 9 10 <div class="card-body"> 11 <div class="create-items"> 12 <div class="form"> 13 <form action="/posts" method="POST"> 14 @csrf 15 16 <div class="input-form"> 17 <label for="name">Name</label> 18 <input name="name"> 19 </div> 20 21 <div class="input-form"> 22 <label for="image">Image</label> 23 <input type="file" name="image"> 24 </div> 25 26 <div class="input-form"> 27 <label for="age">Age</label> 28 <input type="number" name="age" min="0"> 29 </div> 30 31 <div class="input-form"> 32 <label for="gender">Gener</label> 33 <select name="gender"> 34 <option value="men">オス</option> 35 <option value="women">メス</option> 36 </select> 37 </div> 38 39 <div class="input-form"> 40 <label for="explanation">Explanation</label> 41 <textarea name="explanation" rows="4" cols="40"></textarea> 42 </div> 43 44 <div class="input-form"> 45 <input type="submit" value="Submit"> 46 </div> 47 48 </form> 49 </div> 50 </div> 51 </div> 52 </div> 53 </div> 54 </div> 55</div> 56@endsection
web.php
1<?php 2 3use Illuminate\Support\Facades\Route; 4use App\Http\Controllers\PostsController; //投稿機能 5 6/* 7|-------------------------------------------------------------------------- 8| Web Routes 9|-------------------------------------------------------------------------- 10| 11| Here is where you can register web routes for your application. These 12| routes are loaded by the RouteServiceProvider and all of them will 13| be assigned to the "web" middleware group. Make something great! 14| 15*/ 16 17Route::get('/', function () { 18 return view('home'); 19 //return view('welcome'); 20}); 21 22//Sass(デザイン) 23URL::forceScheme('https'); 24 25//トップページ 26Route::get('home',[\App\Http\Controllers\HomeController::class, 'home']); 27//ユーザー 28Auth::routes(); 29//サインアップ・ログイン後の遷移 30Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); 31 32//投稿 33Route::get('/create', [App\Http\Controllers\PostController::class, 'create'])->name('post.create'); 34Route::get('/posts', [App\Http\Controllers\PostController::class, 'store'])->name('post.store'); 35Route::post('/posts', [App\Http\Controllers\PostController::class, 'store'])->name('post.store'); 36 37//投稿を押した時 38Route::post('/post', [App\Http\Controllers\PostController::class, 'store'])->name('post.store');
試したこと
上記のエラーの前にThe GET method is not supported for route posts. Supported methods:
というエラーが発生したので、エラーを調べて、web.phpのstoreに関連するpostの記述の前にgetの記述も書き加えたところ、上記のエラーが発生しました。
補足情報(FW/ツールのバージョンなど)
php: 8.1.21
laravel: 10.18.0

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。