観光地の一覧が表示されるサイトを作成しています。表示される観光地の一覧に対してレビュー機能を実装したいのですが、ログインしているuser_idがPostreviewテーブルに挿入されず、下記のエラーが出てしまい,解決できません。アドバイスをいただきたいです。
❇︎画像1枚目はpost_reviewテーブルです。
local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into post_reviews
(post_id
, id
, user_id
, stars
, comment
, updated_at
, created_at
) values (?, ?, ?, ?, aaa, 2022-02-14 11:23:52, 2022-02-14 11:23:52))
PostController.php
public function review(Request $request) { $review = new \App\PostReview(); $review->post_id = $request->post_id; $review->id = $request->id; $review->user_id= Auth::user(); $review->comment = $request->comment; $result = $review->save(); return ['result' => $result]; }
index.vue
<template> <v-row justify="center"> <div class="item"> <div class="name"> <div class="post_name"> <p>{{ post.name }}</p> </div> <div class="post_image"> {{ post.content }}<img v-bind:src="post.path" class="image" /> </div> </div> <div class="open"> <h1>営業時間</h1> <div class="openning_hour">{{ post.openning_hour }}</div> </div> <div class="address_box"> <h2>住所</h2> <div class="adress">{{ post.adress }}</div> </div> <div class="access_box"> <h3>アクセス</h3> <div class="access">{{ post.access }}</div> </div> <div class="official_url_box"> <h3>公式url</h3> <a :href="post.official_url" target="_blank" rel="noopener" class="image" >{{ post.official_url }}</a > </div> <div class="googlemap_box"> <div class="access"><iframe v-bind:src="post.googlemap"></iframe></div> </div> <v-btn @click="open">レビュー投稿</v-btn> <div class="dialog"> <v-dialog v-model="dialog"> <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">×</span> </button> <div class="modal-body"> <div class="form-group"></div> <div class="form-group"> <h4>コメント</h4> <textarea class="form-control" v-model="reviewParams.comment" ></textarea> </div> </div> <button type="button" class="btn btn-link mr-2" data-dismiss="modal"> 閉じる </button> <button type="button" class="btn btn-warning" @click="onSubmit"> 登録する </button> </v-dialog> </div> </div> </v-row> </template> <script> import axios from "axios"; export default { created() { axios .get(`http://127.0.0.1:8000/api/category/post/${this.$route.params.id}`) .then(response => { this.post = response.data.post; this.id = response.data.id; this.name = response.data.name; this.path = response.data.path; this.openning_hour = response.data.oppenning_hour; this.access = response.data.adress; this.official_url = response.data.official_url; this.googlemap = response.data.googlemap; this.comment = response.data.comment; console.log(response.data.post); console.log(response.data.comment); }); }, data() { return { userId: parseInt("{{ auth()->user()->id ?? -1 }}"), params: "", post: [], id: "", name: "", content: "", path: "", opening_hour: "", adress: "", official_url: "", googlemap: "", review: "", post: "", stars: "", star: "", comment: "", dialog: "", reviewParams: { post_id: "", comment: "", post: "" } }; }, methods: { returnPage() { this.$router.go(-1); }, jump: function(id) { this.$router.push({ path: `/${id}` }); console.log(id); }, onSubmit(postId) { axios .post(`http://127.0.0.1:8000/api/review`, this.reviewParams) .then(response => { if (response.data.result === true) { this.getProducts(); $("#review-modal").modal("hide"); } }); }, open: function() { this.dialog = true; }, openReviewForm(postId) { this.reviewParams = { post_id: "", comment: "", post: "", user_id: "" }; } } }; </script>
PostReview.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class PostReview extends Model { protected $fillable = [ 'id','comment','user_id', ]; public function user() { return $this->belongsTo(\App\User::class, 'user_id', 'id') ->select('id', 'name'); } public function index() { $posts = Post::all(); return response()->json(['posts'=>$posts],200); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/02/16 17:37