In my project I use the default App\User.php model like this:
namespace App;
class User extends Authenticatable
{
public function comments()
{
return $this->hasMany('App\Comment', 'user_id', 'id');
}
}
I have created a package "shop" and want to extend the User model. For example:
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');
}
}
If I do
$wishlist = \Auth::user()->cart()->with(['posts' => function($subquery){
return $subquery->wherePivot('listtype', 'wishlist');
}])->first();
it returns "Call to undefined method App\User::cart()". Because it's an optional package I don't want to put the methods in \App\User.php but in the package where it belongs. Is there another way around?
App\UserbutIlluminate\Foundation\Auth\User. Plus, extending doesn't use your class anywhere in code