6

Folder Structure:

app
|-Admin.php
|-Admin
     |
     |-Product.php





    Admin.php
    --------------------------------------------------------
    namespace App;

    use Illuminate\Foundation\Auth\User as Authenticatable;

    class Admin extends Authenticatable
    {
        public function erp()
        {
            return $this->belongsToMany(Admin\Product::class);
        }
    }
    -----------------------------------------------------------



    Product.php
    -----------------------------------------------------------
    namespace App\Admin;

    use Illuminate\Database\Eloquent\Model;

    class Product extends Model
    {

        protected $primaryKey = 'productcode';
        public $incrementing = false;

        public function updatedBy()
        {
            return $this->belongsTo(Admin::class);
        }
    }

-----------------------------------------------------------

But got an error Class 'Admin::class' not found any solution??

2 Answers 2

6

Use correct namespaces:

Admin model:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Admin\Product;

class Admin extends Authenticatable
{
    public function erp()
    {
        return $this->belongsToMany(Product::class);
    }
}

Product model:

namespace App\Admin;

use Illuminate\Database\Eloquent\Model;
use App\Admin;

class Product extends Model
{

    protected $primaryKey = 'productcode';
    public $incrementing = false;

    public function updatedBy()
    {
        return $this->belongsTo(Admin::class);
    }
}

Or add namespace of a model as string, for example App\Admin

Sign up to request clarification or add additional context in comments.

Comments

3

Please see your name space

Product.php
-----------------------------------------------------------
namespace App\Admin;

So when you call Admin::class it mean App\Admin\Admin::class So please change your relation like this.

class Product extends Model
{

    protected $primaryKey = 'productcode';
    public $incrementing = false;

    public function updatedBy()
    {
        return $this->belongsTo('App\Admin');
    }
}

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.