質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

Laravel 6

Laravel 6は、オープンソースなPHPのフレームワーク。Webアプリケーションの開発に適しており、バージョン6はLTSです。5.8での向上に加えて、セマンティックバージョニングの採用やLaravel Vaporとのコンパチビリティなどが変更されています。

Q&A

解決済

1回答

688閲覧

レビュー投稿時のuser_idの取得

退会済みユーザー

退会済みユーザー

総合スコア0

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

Laravel 6

Laravel 6は、オープンソースなPHPのフレームワーク。Webアプリケーションの開発に適しており、バージョン6はLTSです。5.8での向上に加えて、セマンティックバージョニングの採用やLaravel Vaporとのコンパチビリティなどが変更されています。

0グッド

0クリップ

投稿2022/02/14 14:28

観光地の一覧が表示されるサイトを作成しています。表示される観光地の一覧に対してレビュー機能を実装したいのですが、ログインしている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">&times;</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); } }

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

diff

1public function review(Request $request) 2{ 3 $review = new \App\PostReview(); 4 $review->post_id = $request->post_id; 5 $review->id = $request->id; 6- $review->user_id= Auth::user(); 7+ $review->user_id= Auth::id(); 8 $review->comment = $request->comment; 9 $result = $review->save(); 10 return ['result' => $result]; 11 }

または

diff

1public function review(Request $request) 2{ 3 $review = new \App\PostReview(); 4 $review->post_id = $request->post_id; 5 $review->id = $request->id; 6- $review->user_id= Auth::user(); 7+ $review->user_id= $request->user()->id; 8 $review->comment = $request->comment; 9 $result = $review->save(); 10 return ['result' => $result]; 11 }

投稿2022/02/14 15:23

phper.k

総合スコア3923

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/02/16 17:37

返信ありがとうございます。指定していただいたように修正したら、解決できましたありがとうございます🙇‍♂️
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問