実現したいこと
Udemyで以下の講座を受講しており、
https://www.udemy.com/course/laravel-vue3-crm/learn/lecture/33118760#questions
vue.jsのLinkからLaravelへstoreする箇所で入力値がstoreに渡る箇所がnullになっており、原因がわからず困っています。解決したいです。
発生している問題・分からないこと
入力値がnullで渡り、エラーとなっています。
エラーメッセージ
error
1Illuminate\Database\QueryException 2SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: inertia_tests.title 3INSERT INTO 4 "inertia_tests" ("title", "content", "updated_at", "created_at") 5VALUES 6 (?, ?, 2024 -04 -27 08: 58: 24, 2024 -04 -27 08: 58: 24)
該当のソースコード
```web.php(一部) use Inertia\Inertia; use App\Http\Controllers\InertiaTestController; Route::get('/inertia/index', [InertiaTestController::class, 'index'])->name('inertia.index'); Route::post('/inertia', [InertiaTestController::class, 'store'])->name('inertia.store'); Route::get('/inertia/show/{id}', [InertiaTestController::class, 'show'])->name('inertia.show');
InertiaTest.vue
1<script setup> 2import { Head, Link } from '@inertiajs/vue3'; 3import { ref } from 'vue' 4 5const newTitle = ref('') 6const newContent = ref('') 7 8</script> 9 10<template> 11Inertiaテストです。<br> 12<a href="/">aタグ経由です</a><br> 13<Link href="/">Link経由です</Link><br> 14<Link :href="route('inertia.index')">名前つきルートの確認です</Link><br> 15<Link :href="route('inertia.show', { id: 50 })">ルートパラメータのテストです</Link> 16 17<div class="mb-8"></div> 18<input type="text" name="newTtile" v-model="newTitle" />{{ newTitle }}<br> 19<input type="text" name="newContent" v-model="newContent" />{{ newContent }}<br> 20<Link as="button" method="post" :href="route('inertia.store')" 21:date="{ 22 title: newTitle, 23 content: newContent 24}">DB保存テスト</Link> 25</template>
InertiaTestController.php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use Inertia\Inertia; 7use App\Models\InertiaTest; 8 9class InertiaTestController extends Controller 10{ 11 public function index() { 12 return inertia::render('Inertia/Index'); 13 } 14 15 public function show($id) { 16 return Inertia::render('Inertia/Show', 17 [ 18 'id' => $id 19 ]); 20 } 21 22 public function store(Request $request) { 23 $InertiaTest = new InertiaTest; 24 $InertiaTest->title = $request->title; 25 $InertiaTest->content = $request->content; 26 $InertiaTest->save(); 27 28 return to_route('inertia.index'); 29 } 30}
InertiaTest.php
1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Factories\HasFactory; 6use Illuminate\Database\Eloquent\Model; 7 8class InertiaTest extends Model 9{ 10 use HasFactory; 11}
### 試したこと・調べたこと - [x] teratailやGoogle等で検索した - [ ] ソースコードを自分なりに変更した - [ ] 知人に聞いた - [ ] その他 ##### 上記の詳細・結果 chatGPTで調べてみましたが、解決に至りませんでした。 ### 補足 http://127.0.0.1:8000/inertia-testにアクセスして、 titleとcontentのinputに値を入力し、更新をかけるとエラーになります。 storeメソッドまでは行っており、dd($request->title)を見るとたしかにnullになっています。 どうぞご指導のほど、よろしくお願いいたします。 追記1 ちなみに、$requestには何もはいっていませんでした。 (storeでdd($request)しました) ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2024-04-27/def8d13d-5ac4-4cfb-a143-c4fe65f93d58.png) 追記2 nameに綴り間違いがあったので、直したのですが、うまくいかないままです。 newを取ってもうまくいきませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。