使用 Spate 媒体库时出错:"BadMethodCallException Call to undefined method Illuminate\Foundation\Auth\User::addMediaFromRequest()"
Error when using Spate Media Library: "BadMethodCallException Call to undefined method Illuminate\Foundation\Auth\User::addMediaFromRequest()"
这是我的用户模型
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\File;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements HasMedia
{
use HasMediaTrait;
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role(){
return $this->belongsToMany('App/Role');
}
}
这是我的用户控制器
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
我想使用 Spatie 媒体库并为每个用户上传一张照片,但我收到与 Spatie 媒体库相关的错误:
"Call to undefined method
Illuminate\Foundation\Auth\User::addMediaFromRequest()".
我已经阅读了一些相关的帖子,但我不明白如何解决这个问题。
谢谢。
库的第 8 版有此文档。您使用的特征正确吗?
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class YourModel extends Model implements HasMedia
{
use InteractsWithMedia;
}
已编辑:
另一方面,使用 HasMediaTrait
适用于版本 7,因此我假设您使用的是版本 7。我认为这是因为您导入了错误的 User
class。用于实现HasMedia
的User
class位于App\User
命名空间中。但是您正在从 Illuminate\Foundation\Auth\User
命名空间导入 User
class。所以将其更改为 use App\User;
应该没问题。
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
https://docs.spatie.be/laravel-medialibrary/v8/basic-usage/preparing-your-model/
这是我的用户模型
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\File;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements HasMedia
{
use HasMediaTrait;
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role(){
return $this->belongsToMany('App/Role');
}
}
这是我的用户控制器
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
我想使用 Spatie 媒体库并为每个用户上传一张照片,但我收到与 Spatie 媒体库相关的错误:
"Call to undefined method Illuminate\Foundation\Auth\User::addMediaFromRequest()".
我已经阅读了一些相关的帖子,但我不明白如何解决这个问题。 谢谢。
库的第 8 版有此文档。您使用的特征正确吗?
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class YourModel extends Model implements HasMedia
{
use InteractsWithMedia;
}
已编辑:
另一方面,使用 HasMediaTrait
适用于版本 7,因此我假设您使用的是版本 7。我认为这是因为您导入了错误的 User
class。用于实现HasMedia
的User
class位于App\User
命名空间中。但是您正在从 Illuminate\Foundation\Auth\User
命名空间导入 User
class。所以将其更改为 use App\User;
应该没问题。
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
https://docs.spatie.be/laravel-medialibrary/v8/basic-usage/preparing-your-model/