此路由不支持 POST 方法。调用未定义的方法 Faker\Provider\Image::make()
The POST method is not supported for this route. Call to undefined method Faker\Provider\Image::make()
我在看这个视频:https://www.youtube.com/watch?v=jy2SUxx6uHc&t=402s
基本上我正在尝试在用户中添加个人资料照片,但没有用。
它显示了对未定义方法 Faker\Provider\Image::make() 的调用
调用未定义的方法 Faker\Provider\Image::make()
我正在使用图像干预 (http://image.intervention.io/) 和 bootstrap。
请帮助我:)
我的代码:
profile.blade.php
@extends('layouts.index')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<img src="/uploads/avatars/{{ $user->avatar }}"
style="width: 150px; height: 150px; float: left; border-radius: 50%; margin-right: 25px;"
alt="Profile picture">
<h2>{{ $user->name }}'s Profile</h2>
<form enctype="multipart/form-data" action="{{ url('profile') }}" method="POST">
<label>Update Profile Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="pull-right btn btn-sm btn-primary">
</form>
</div>
</div>
</div>
@endsection
UserController.php->
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Faker\Provider\Image;
class UserController extends Controller
{
//
public function profile(){
return view('profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request){
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
}
web.php
<?php
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');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('profile', 'UserController@profile');
Route::post('profile', 'UserController@update_avatar');
我会给出答案(感谢@apokryfos、@Tim Lewis 和@Immortal Dude 的帮助)。
所以首先我需要改变路线,这是正确的:
Route::post('profile', 'UserController@update_avatar')->name('profile');
其次我改变了UserController上的用法,这是对的:
use Intervention\Image\Facades\Image;
最后一个要改的是action,这个是对的:
action="{{ route('profile') }}"
我在看这个视频:https://www.youtube.com/watch?v=jy2SUxx6uHc&t=402s 基本上我正在尝试在用户中添加个人资料照片,但没有用。 它显示了对未定义方法 Faker\Provider\Image::make() 的调用 调用未定义的方法 Faker\Provider\Image::make()
我正在使用图像干预 (http://image.intervention.io/) 和 bootstrap。 请帮助我:)
我的代码:
profile.blade.php
@extends('layouts.index')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<img src="/uploads/avatars/{{ $user->avatar }}"
style="width: 150px; height: 150px; float: left; border-radius: 50%; margin-right: 25px;"
alt="Profile picture">
<h2>{{ $user->name }}'s Profile</h2>
<form enctype="multipart/form-data" action="{{ url('profile') }}" method="POST">
<label>Update Profile Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="pull-right btn btn-sm btn-primary">
</form>
</div>
</div>
</div>
@endsection
UserController.php->
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Faker\Provider\Image;
class UserController extends Controller
{
//
public function profile(){
return view('profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request){
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
}
web.php
<?php
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');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('profile', 'UserController@profile');
Route::post('profile', 'UserController@update_avatar');
我会给出答案(感谢@apokryfos、@Tim Lewis 和@Immortal Dude 的帮助)。
所以首先我需要改变路线,这是正确的:
Route::post('profile', 'UserController@update_avatar')->name('profile');
其次我改变了UserController上的用法,这是对的:
use Intervention\Image\Facades\Image;
最后一个要改的是action,这个是对的:
action="{{ route('profile') }}"