Sab*_*osh 3 laravel laravel-5.3 laravel-eloquent laravel-5.4
资料夹结构:
应用程式
| -Admin.php
|-管理员
|
| -Product.php
Admin.php
-------------------------------------------------- ------
命名空间应用;
使用Illuminate \ Foundation \ Auth \ User作为Authenticateable;
管理员类扩展了Authenticateable
{
公共功能erp()
{
返回$ this-> belongsToMany(Admin \ Product :: class);
}
}
-------------------------------------------------- ---------
Product.php
-------------------------------------------------- ---------
命名空间App \ Admin;
使用Illuminate \ Database \ Eloquent \ Model;
类产品扩展模型
{
protected $ primaryKey ='产品代码';
public $ incrementing = false;
公共函数updateBy()
{
返回$ this-> belongsTo(Admin :: class);
}
}
-------------------------------------------------- ---------
但是出现错误Class'Admin :: class'找不到任何解决方案?
使用正确的名称空间:
管理员模型:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
产品型号:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
或将模型的名称空间添加为字符串,例如 App\Admin