5
votes

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

2 Answers

6
votes

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

3
votes

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');
    }
}