前提・実現したいこと
Docker for Macを用いて、Laravelで作ったアプリをHerokuにデプロイしました。
ローカルでは通常通りの挙動をするのですが、Heroku上で開くとエラーが表示されます。
Heroku上でもローカルと同じように動作するようにしたいです。
発生している問題・エラーメッセージ
heroku open
を打つと次のようなエラーメッセージが表示されます。
Undefined variable: profile (View: /app/resources/views/stories/index2.blade.php)
該当のソースコード
resources/views/stories/Index2.blade.php
php
1@extends('layouts.front2') 2@section('title','mainpage') 3 4@section('content') 5 6<link rel="stylesheet" href="{{ asset('css/main2.css') }}"> 7 8<div class="profile"> 9 10 11 <div class="name"> 12 @guest 13 <a class="nav-link2" href="{{ route('register')}}">{{ __('Create Accout!')}}</a> 14 @else 15 <a id="navbarDropdown" class="nav-link2" href="#" role="button"> 16 {{ Auth::user()->name }}<span class="caret"></span></a> 17 <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> 18 @csrf 19 </form> 20 @endguest 21 </div> 22 23<div class="aboutme"> 24 <tbody> 25 @foreach($posts as $profile) 26 <tr> 27 <td>{{ \Str::limit($profile->title, 100) }}</td> 28 <td>{{ \Str::limit($profile->body, 250) }}</td> 29 </tr> 30 <a href="{{ action('ProfileController@delete', ['id' => $profile->id]) }}">delete</a> 31 <a href="{{ action('ProfileController@update', ['id' => $profile->id]) }}" class="update">update</a> 32 @endforeach 33 34</tbody> 35<br> 36</div> 37</div> 38 39<div class="new"> 40 41 <div class="newtitle"> 42 <h1>New</h1> 43 </div> 44 45 <div class="container1"> 46 47 @foreach ($images as $image) 48 <img src="/storage/{{ $image->path . $image->name }}" class="images" style="height: 250px; width: 250px; border-radius: 50%;"> 49 <a href="{{ action('StoriesController@delete', ['id' => $image->id]) }}">delete</a> 50 @endforeach 51 52 <div class="more"> 53 more... 54 </div> 55 </div> 56 57</div> 58 59 60{{ csrf_field() }} 61@endsection
app/Http/Controllers/ProfileController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Stories; use App\History; use App\Posts; use App\Profile; use Carbon\Carbon; use Storage; class ProfileController extends Controller { public function index(Request $request) { $cond_title = $request -> cond_title; if ($cond_title != ''){ $profile = Profile::where('title', $cond_title)->get(); } else { $profile = Profile::all(); } return view('stories.index2', ['profile' => $profile, 'cond_title' => $cond_title]); } public function upload(Request $request) { $this->validate($request, [ 'file' => [ 'required', 'file', 'image', 'mimes:jpeg,png', ] ]); if ($request->file('file')->isValid([])) { $path = $request->file->store('public'); return view('stories.index2')->with(compact('filename','plus')); } else { return redirect() ->back() ->withInput() ->withErrors(); } } public function store(Request $request) { $d = new \DateTime(); $d->setTimeZone(new \DateTimeZone('Asia/Tokyo')); $dir = $d->format('Y/m'); $path = sprintf('public/posts/%s', $dir); $data = $request->except('_token'); foreach ($data['plus'] as $k => $v) { $filename = ''; $posts = Posts::take(1)->orderBy('id', 'desc')->get(); foreach ($posts as $post) { $filename = $post->id + 1 . '_' . $v->getClientOriginalName(); } unset($post); if ($filename == false) { $filename = 1 . '_' . $v->getClientOriginalName(); } $v->storeAs($path, $filename); $post_data = [ 'path' => sprintf('posts/%s/', $dir), 'name' => $filename ]; $a = new Posts(); $a->fill($post_data)->save(); } unset($k, $v); return redirect('/'); } public function create(Request $request) { $this->validate($request, Profile::$rules); $profile = new Profile; $form = $request->all(); unset($form['_token']); $profile->fill($form)->save(); return redirect('/'); } public function add() { return view('profile.create2'); } public function index3() { return view('profile.create3'); } public function edit(Request $request) { $profile = Profile::find($request->id); if (empty($profile)) { abort(404); } return view('profile.create4', ['profile_form' => $profile]); } public function delete(Request $request) { // 該当するNews Modelを取得 $profile = Profile::find($request->id); // 削除する $profile->delete(); return redirect('/'); } public function update(Request $request) { $this->validate($request, Profile::$rules); $profile = Profile::find($request->id); $profile_form = $request->all(); unset($profile_form['_token']); $profile->fill($profile_form)->save(); return redirect('/'); } }
app/Http/Controllers/StoriesController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Story; use App\Profile; use Auth; use App\Posts; use App\History; use App\Attachment; use Carbon\Carbon; use Storage; class StoriesController extends Controller { public function __construct() { $this->middleware('auth'); } public function index(Request $request) { $images = Attachment::all(); $cond_title = $request->cond_title; if ($cond_title != '') { // 検索されたら検索結果を取得する $posts = Profile::where('title', $cond_title)->get(); } else { // それ以外はすべてのニュースを取得する $posts = Profile::all(); } return view('stories.index2', compact('images','posts','cond_title')); } public function add() { return view('stories.create2'); } public function store(Request $request) { $d = new \DateTime(); $d->setTimeZone(new \DateTimeZone('Asia/Tokyo')); $dir = $d->format('Y/m'); $path = sprintf('public/images/%s', $dir); $data = $request->except('_token'); foreach ($data['images'] as $k => $v) { $filename = ''; $attachments = Attachment::take(1)->orderBy('id', 'desc')->get(); foreach ($attachments as $attachment) { $filename = $attachment->id + 1 . '_' . $v->getClientOriginalName(); } unset($attachment); if ($filename == false) { $filename = 1 . '_' . $v->getClientOriginalName(); } $v->storeAs($path, $filename); $attachment_data = [ 'path' => sprintf('images/%s/', $dir), 'name' => $filename ]; $a = new Attachment(); $a->fill($attachment_data)->save(); } unset($k, $v); return redirect('/'); } public function delete(Request $request) { // 該当するNews Modelを取得 $images = Attachment::find($request->id); // 削除する $images->delete(); return redirect('/'); } public function upload(Request $request) { $this->validate($request, [ 'file' => [ 'required', 'file', 'image', 'mimes:jpeg,png', ] ]); if ($request->file('file')->isValid([])) { $path = $request->file->store('public'); return view('stories.index2')->with('filename', basename($path)); } else { return redirect('/') ->back() ->withInput() ->withErrors(); } } }
試したこと
https://qiita.com/shosho/items/b31a4757e223e2261a27
こちらのサイトを参考にし、index2→Index2とファイル名を大文字に変更しました。
補足情報(FW/ツールのバージョンなど)
MacOS Catalina 10.15.4
Docker for Mac version 2.2.0.5
PHP 7.3.11
Composer 1.9.1
heroku/7.39.2 darwin-x64 node-v12.13.0
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー