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');
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/05/23 16:22
2020/05/23 17:25 編集
退会済みユーザー
2020/05/23 21:31