お世話になってます。PHP初心者です。
今、フォームから画像をアップロードするものを
作成しております。
確かに画像を所定の場所(public\item\photos)にアップロードできるのですが、
入力規則及びページネーションが上手く入りません。
以前、同類のやり方で上手くできたのですが、何故か今回は入りません。
ご教示お願いできませんでしょうか?
<やりたいこと>
フォームに入力規則及びページネーションを入れる
<エラー名>
Method Illuminate\Database\Eloquent\Collection::render does not exist
(Collectionクラスにrenderメソッドはない)
<やったこと>
$ehime->render()や$ehime->links()で試したがダメ
<参考サイト>
①Laravelのvalidationメソッドでバリデーションを実装する入門編(日本語エラーメッセージ付き)
https://www.ritolab.com/entry/40
②laravel5.5を使用して複数画像をアップロードする方法
https://qiita.com/netfish/items/ef01cdb5f58742563e87
<記入内容>
web.php
Route::get('items', 'ItemController@index'); Route::match(['GET', 'POST'], '/create', 'ItemController@create');
form(create.blade.php)
@extends('layouts.master_request') @section('title', 'Laravel tutorial(Biginner)') @section('content') @if (count($errors) > 0) <ul> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> @endif <!-- フォーム --> <h3>Form_Sample(Enter_Screen)</h3> <p>Please enter items</p> <form action="{{ url('create') }}" method="POST" enctype="multipart/form-data"> <label for="jan">Jan_Code:</label> <input type="text" class="form-control" name="jan" value="{{old('jan')}}"> <br> <label for="name">Item_name</label> <input type="text" class="form-control" name="name" value="{{old('name')}}"> <br> <label for="photo">Image_File(Multiple possible)</label> <input type="file" class="form-control" name="files[][photo]" multiple> <br> <hr> {{ csrf_field() }} <button class="btn btn-success"> Upload </button> </form> <table class="table table-striped table-hover"> <thead> <tr> <th>No</th> <th>name</th> </tr> </thead> <tbody> @foreach($ehime as $item) <tr> <td>{{$item->item_id}}</td><td>{{$item->path}}</td> </tr> @endforeach </tbody> </table> <!-- page control --> {!! $ehime->render() !!} @endsection
Controller(ItemController.php)
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Item; use App\ItemPhoto; use App\Http\Requests\ItemRequest; class ItemController extends Controller{ public function index() { $items = ItemPhoto::all(); return view('item.create')->with('ehime',$items); } public function create(Request $request) { // POST if($request->isMethod('POST')) { // 商品情報の保存 $item = Item::create(['jan'=> $request->jan, 'name'=> $request->name]); // 商品画像の保存 foreach ($request->file('files') as $index=> $e) { $ext = $e['photo']->guessExtension(); $filename = "{$request->jan}_{$index}.{$ext}"; $path = $e['photo']->storeAs('photos', $filename); // photosメソッドにより、商品に紐付けられた画像を保存する $item->photos()->create(['path'=> $path]); } return redirect()->action('ItemController@index'); } // GET return view('item.create'); } }
バリデーション(ItemRequest.php)
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class ItemRequest extends FormRequest{ public function authorize() { return true; } public function rules() { if($this->isMethod('get')) return[]; return [ 'jan'=>'required', 'name'=>'required', 'file.*.photo'=>'required|image|mimes:jpeg,png,jpg,gif', ]; } public function messages() { return [ "required" => "Required items", "image" => "Specified files is not image files!!", "mines" => "Extension of specified files is not PNG or JPG or GIF!!", ]; } }

回答2件
あなたの回答
tips
プレビュー