SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: votings.name (SQL: insert into "votings" ("name", "updated_at", "created_at") values (?, 2020-12-17 17:35:44, 2020-12-17 17:35:44))
現在トップページから文章を投稿するためのコードを記載しているのですが、上記のエラーが出てしまい戸惑っています。
テーブル名:votings
モデル名:Voting
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateVotingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('votings', function (Blueprint $table) {
$table->id();
$table->String('name');
$table->String('article');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('votings');
}
}
web.php
<?php
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Route;
use App\Models\Voting;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
/* Votingモデルを全て取得して表示する。 */
$votings = Voting::all();
return view('votings', ['votings' => $votings]);
});
//Postはデータを送信するという意味
Route::post('/voting', function (Request $request) {
$validator = Validator::make($request->all(), [
'result' => 'required|max:255',
]);
$voting = new Voting;
$voting->name = $request->result;
$voting->save();
return redirect('/');
});
votings.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="col-sm-offset-2 col-sm-8">
<div class="panel panel-default">
<div class="panel-heading">
新しい記事
</div>
<div class="panel-body">
@include('common.errors')
<!-- 新しい投稿を作る -->
<form action="/voting" method="POST" class="form-horizontal">
{{ csrf_field() }}
<!-- 記事の名前 -->
<div class="form-group">
<label for="task-name" class="col-sm-3 control-label">記事</label>
<div class="col-sm-6">
<input type="text" name="name" id="voting-name" class="form-control" value="{{ old('voting')}}">
</div>
</div>
<!-- add voting -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-plus"></i>記事を追加する
</button>
</div>
</div>
</form>
</div>
</div>
<!-- 記事一覧 -->
@if (count($votings)>0)
<div class="panel-body">
<div class="panel-heading">
記事一覧
</div>
<div class="panel-body">
<table class="table table-striped task-table">
<thead>
<th>記事</th>
<th> </th>
</thead>
<tbody>
@foreach ($votings as $voting)
<tr>
<td class="table-text">
<div class="table-text">{{$voting->name}}</div>
</td>
<!-- 記事削除 -->
<td>
<form action="/voting/{{ $voting->id}}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">
<i class="fa fa-trash"></i>削除
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endif
</div>
</div>
@endsection
処理としては$table->String('name')というテーブルに記事を追加するというボタンを押したら値を追加しトップページに表示するという流れなのですが、、お力をお貸しください。
0 likes