如何建立模型 class 与 laravel eloquent 中文件夹的另一个模型 class 的关系?

How can I establish a relationship a Model class with another model class of folder in laravel eloquent??

文件夹结构:

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

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

但出现错误Class 'Admin::class' 未找到 有什么解决办法吗??

请看你的名字space

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

所以当你调用 Admin::class 时,它的意思是 App\Admin\Admin::class 所以请像这样改变你的关系。

class Product extends Model
{

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

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

使用正确的命名空间:

管理模式:

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

产品型号:

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

或者将模型的命名空间添加为字符串,例如App\Admin