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

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

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

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

5784閲覧

laravel レビュー機能

退会済みユーザー

退会済みユーザー

総合スコア0

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2020/05/23 16:01

laravelで食べログみたいなアプリを開発中です。
今お店のレビュー機能を実装しようとして困っております。

具体的には
Reviewテーブルにuser_idとshop_idの外部キーを用意しています。
shop.showにreview.createにいくaタグをつけてviewを飛ばしております。
レビューを書き保存する際(review.storeで)user_idは取ってこれるのですが、shop_idは取ってこれません。
どうすればreview.store時にshop_idを持ってくることができるでしょうか?

laravelで初めてアプリを作っているので、コードが汚いかもしれません

User.php

<?php namespace App; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; public function shops() { return $this->hasMany('App\Shop', 'published_shop_id'); } public function reviews() { return $this->hasMany('App\Review'); } public function posts() { return $this->hasMany('App\Post'); } public function reservation() { return $this->belongsTo('App\Reservation'); } // public function commodities() // { // return $this->belongsToMany('App\commodity'); // } }

Shop.php

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Shop extends Model { protected $fillable = [ 'name', 'region', 'price', 'detail', 'store_in', 'take_out', 'delivery', ]; public function commodities() { return $this->hasMany('App\Commodity'); } public function images() { return $this->hasMany('App\Image'); } public function reviews() { return $this->hasMany('App\Review'); } public function posts() { return $this->hasMany('App\Post'); } public function user() { return $this->belongsTo('App\User', 'foreign_key'); } public function reservation() { return $this->belongsTo('App\Reservation'); } }

Review.php

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Review extends Model { protected $fillable = [ 'evaluation', 'detail', ]; public function shop() { return $this->belongsTo('App\Shop'); } public function user() { return $this->belongsTo('App\User'); } }

shop.show

@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">一覧</div> <div class="container mt-4"> <div class="card mb-4"> <div class="card-header"> {{ $shop->name }} </div> <div class="card-body"> <p class="card-text"> {{ $shop->price }} </p> </div> <a class="shop-news" href="{{ $shop->id }}/edit"> 店を編集する </a> <a class="review-new" href="review/create"> レビューを書く </a> </div> </div> </div> </div> </div> </div> </div> @endsection

ReviewController

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Review; use App\Shop; class ReviewController extends Controller { public function create() { // $shop = Shop::findOrFail($id); // echo var_dump($id); // return view('shop.edit', ['shop' => $shop]); return view('review/create'); } public function store(Request $request) { $review = new Review; $review->evaluation = $request->input('evaluation'); $review->detail = $request->input('detail'); // $review->user_id = $request->user()->id; // $review->save(); $shop = Shop::find($request->id); $review->shop_id = $shop; $review->save(); // echo var_dump($shop); return redirect('/'); } }

web.php

<?php use App\Http\Controllers\UserController; use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | 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 () { // return view('welcome'); // }); Route::get('/', 'PostController@index'); Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); Route::get('/User/Shop/create', 'ShopController@create'); Route::get('/User/Shop/edit', 'ShopController@edit'); Route::get('/User/{$id}', 'UserController@show'); Route::get('/Shop/{$id}', 'ShopController@show'); Route::get('/Shop/{$id}/edit', 'ShopController@edit'); Route::post('/Shop/update', 'ShopController@update'); Route::get('/Shop/review/create', 'ReviewController@create'); Route::get('User/Post/all', 'PostController@all'); Route::delete('Post/destroy/{$id}', 'PostController@destroy'); Route::resource('User', 'UserController'); Route::resource('Shop', 'ShopController'); Route::resource('Post', 'PostController'); Route::resource('Review', 'ReviewController');

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

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

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

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

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

guest

回答1

0

ベストアンサー

一例ですが、お店の編集と同じく、shop_idをurlに加えておき、create時にhiddenなどで持っておくといいのではないでしょうか。

投稿2020/05/23 16:09

hayato7

総合スコア1135

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

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

退会済みユーザー

退会済みユーザー

2020/05/23 16:22

ご返答ありがとうございます 早速試してみたのですが404エラーが発生してしまいました URLは http://127.0.0.1:8000/Shop/64/review/create とShopテーブルの64番を取ってこれているんですが... コードは下記のようにしました。 shop.show <a class="review-new" href="{{ $shop->id }}/review/create"> レビューを書く </a> web.php Route::get('/Shop/{$id}/review/create', 'ReviewController@create'); 申し訳ありませんが引き続きご享受していただけますと幸いです。
hayato7

2020/05/23 17:25 編集

基本的な渡し方から、知った方が良いと思うので、ひとまずドキュメントのパラメータの渡し方を見てみてください。(ドキュメントのversionはお使いの環境に合わせてください。) https://readouble.com/laravel/5.5/ja/routing.html
退会済みユーザー

退会済みユーザー

2020/05/23 21:31

下記のようにできました。 ありがとうございます。 すごい初歩的なところだったんですね、 もう一度勉強しなおします web.php Route::get('/Shop/{shop_id}/review/create', function (App\Shop $shop_id) { return view('review.create', ['shop_id'=>$shop_id]); });
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問