Laravel扩展App\User并使用\Auth::User()调用;mymethod()
在我的项目中,我使用默认的App\User.php模型,如下所示:Laravel扩展App\User并使用\Auth::User()调用;mymethod(),laravel,model,extend,Laravel,Model,Extend,在我的项目中,我使用默认的App\User.php模型,如下所示: namespace App; class User extends Authenticatable { public function comments() { return $this->hasMany('App\Comment', 'user_id', 'id'); } } 我已经创建了一个包“shop”,并希望扩展用户模型。 例如: namespace Pixiebox\S
namespace App;
class User extends Authenticatable
{
public function comments()
{
return $this->hasMany('App\Comment', 'user_id', 'id');
}
}
我已经创建了一个包“shop”,并希望扩展用户模型。
例如:
namespace Pixiebox\Shop;
use Illuminate\Database\Eloquent\Model;
class Cart extends Authenticatable
{
public function posts()
{
return $this->belongsToMany('App\Post', 'cart_posts', 'cart_id', 'post_id')->withPivot(['listtype', 'qty']);
}
}
Pixiebox\Shop\src\Shop\User.php
namespace Pixiebox\Shop;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function cart()
{
return $this->hasOne('Pixiebox\Shop\Cart', 'user_id', 'id');
}
}
如果我这样做
$wishlist = \Auth::user()->cart()->with(['posts' => function($subquery){
return $subquery->wherePivot('listtype', 'wishlist');
}])->first();
它返回“调用未定义的方法App\User::cart()”。
因为它是一个可选的包,所以我不想将这些方法放在\App\User.php中,而是放在它所属的包中。还有其他方法吗?您可以在
提供者
模型下的配置/auth.php
中设置用于身份验证的:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class, // <---
],
...
“提供者”=>[
“用户”=>[
“司机”=>“雄辩”,
“model”=>App\User::class,//您可以在config/auth.php
中的providers
model下设置用于身份验证的:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class, // <---
],
...
“提供者”=>[
“用户”=>[
“司机”=>“雄辩”,
'model'=>App\User::class,//您实际上并没有扩展App\User
,而是illighted\Foundation\Auth\User
。另外,扩展不会在代码中的任何地方使用您的类您实际上并没有扩展App\User
,而是illighted\Foundation\Auth\User
。此外,扩展不会在代码中的任何地方使用您的类