0

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?

1
  • You don't actually extend App\User but Illuminate\Foundation\Auth\User. Plus, extending doesn't use your class anywhere in code Commented Apr 18, 2020 at 15:31

1 Answer 1

3

You can set within config/auth.php under providers model which is to be used for authentication:

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class, // <---
        ],
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.