###前提・実現したいこと
PHPとLaravelで画像のアップロードをしたいのですが、
getClientOriginalName()でエラーが出ます。
###発生している問題・エラーメッセージ
FatalErrorException in AuthController.php line 67: Call to a member function getClientOriginalName() on string
###該当のソースコード
まずコントローラーです。
php
1<?php 2 3namespace App\Http\Controllers\Auth; 4 5use App\User; 6use Validator; 7use App\Http\Controllers\Controller; 8use Illuminate\Foundation\Auth\ThrottlesLogins; 9use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 10use Image; 11 12class AuthController extends Controller 13{ 14 /* 15 |-------------------------------------------------------------------------- 16 | Registration & Login Controller 17 |-------------------------------------------------------------------------- 18 | 19 | This controller handles the registration of new users, as well as the 20 | authentication of existing users. By default, this controller uses 21 | a simple trait to add these behaviors. Why don't you explore it? 22 | 23 */ 24 25 use AuthenticatesAndRegistersUsers, ThrottlesLogins; 26 27 /** 28 * Where to redirect users after login / registration. 29 * 30 * @var string 31 */ 32 protected $redirectTo = '/'; 33 34 /** 35 * Create a new authentication controller instance. 36 * 37 * @return void 38 */ 39 public function __construct() 40 { 41 $this->middleware('guest', ['except' => 'logout']); 42 } 43 44 /** 45 * Get a validator for an incoming registration request. 46 * 47 * @param array $data 48 * @return \Illuminate\Contracts\Validation\Validator 49 */ 50 protected function validator(array $data) 51 { 52 return Validator::make($data, [ 53 'name' => 'required|max:255', 54 'email' => 'required|email|max:255|unique:users', 55 'password' => 'required|confirmed|min:6', 56 ]); 57 } 58 59 /** 60 * Create a new user instance after a valid registration. 61 * 62 * @param array $data 63 * @return User 64 */ 65 protected function create(array $data) 66 { 67 $fileName = $data['avatar']->getClientOriginalName(); 68 Image::make($data['avatar'])->save(public_path() . '/images/' . $fileName); 69 70 return User::create([ 71 'name' => $data['name'], 72 'email' => $data['email'], 73 'password' => bcrypt($data['password']), 74 'avatar' => $fileName, 75 ]); 76 } 77} 78
フォームです。
php
1@extends('layout') 2 3@section('content') 4<div class="bgcolor-white pt1em pb1em" id="contents"> 5 <div id="main_cnt_wrapper"> 6 <div id="yjContentsBody"> 7 <div class="yjContainer"> 8 <div class="form_box"> 9 <h2>m<span>新規登録</span></h2> 10 {!! Form::open(array('files' => true)) !!} 11 @if (count($errors) > 0) 12 <div id="error_explanation"> 13 <ul> 14 @foreach ($errors->all() as $error) 15 <li>{{ $error }}</li> 16 @endforeach 17 </ul> 18 </div> 19 @endif 20 <div class="label"> 21 {{ Form::label('email') }} 22 {{ Form::email('email', '', ['placeholder' => 'メールアドレスを入力']) }} 23 </div> 24 <div class="label"> 25 {{ Form::label('password') }} 26 {{ Form::password('password', ['placeholder' => 'パスワードを入力']) }} 27 </div> 28 <div class="label"> 29 {{ Form::label('password_confirmation') }} 30 {{ Form::password('password_confirmation', ['placeholder' => 'パスワードを入力(確認)']) }} 31 </div> 32 <div class="label"> 33 {{ Form::label('name') }} 34 {{ Form::text('name') }} 35 </div> 36 <div class="field"> 37 {{ Form::file('avatar') }} 38 </div> 39 40 <div class="submit"> 41 <div class="actions"> 42 {{ Form::submit('Create User', ['class' => 'btn btn--block']) }} 43 </div> 44 </div> 45 46 {!! Form::close() !!} 47 48 <div class="more_link_box"> 49 <strong>すでにアカウントを持っていますか?</strong> 50 <a href="/login">Log in</a> 51 </div> 52 </div> 53 </div> 54 </div> 55 </div> 56@endsection
###補足情報(言語/FW/ツール等のバージョンなど)
windows
php 5.6.8
laravel 5.2
composer 1.4.2